fork(1) download
  1. public class Main {
  2.  
  3. public static void main(String[] args) throws Exception {
  4. //System.setIn(new FileInputStream("input.txt"));
  5. //System.setOut(new PrintStream("output.txt"));
  6. StdIn in = new StdIn();
  7. int n = in.nextInt();
  8. MyStack st = new MyStack();
  9. while (n-- > 0) {
  10. int x = in.nextInt();
  11. if (st.isEmpty() || st.peek() != x) {
  12. st.push(x);
  13. }
  14. }
  15. int m = in.nextInt();
  16. int rank = st.size();
  17. while (m-- > 0) {
  18. int x = in.nextInt();
  19. while (!st.isEmpty() && st.peek() <= x) {
  20. st.pop();
  21. --rank;
  22. }
  23. st.push(x);
  24. System.out.println(++rank);
  25. }
  26. }
  27.  
  28. static class MyStack {
  29. private int capacity;
  30. private int[] data;
  31. private int top;
  32. public MyStack() {
  33. data = new int[capacity = 4];
  34. top = 0;
  35. }
  36. public int size() { return top; }
  37. public boolean isEmpty() { return size() == 0; }
  38. public void push(int x) {
  39. if (top + 1 >= capacity) grow();
  40. data[++top] = x;
  41. }
  42. public int peek() { return data[top]; }
  43. public int pop() { return data[top--]; }
  44. private void grow() {
  45. int[] temp = new int[capacity <<= 1];
  46. System.arraycopy(data, 0, temp, 0, top + 1);
  47. data = temp;
  48. }
  49. }
  50.  
  51. static class StdIn {
  52. final private int BUFFER_SIZE = 1 << 21;
  53. private byte[] buffer;
  54. private int bufferPointer, bytesRead;
  55.  
  56. public StdIn() {
  57. buffer = new byte[BUFFER_SIZE];
  58. bufferPointer = bytesRead = 0;
  59. }
  60.  
  61. public String next() throws Exception {
  62. int c;
  63. while ((c = read()) != -1 && (c == ' ' || c == '\n' || c == '\r'))
  64. ;
  65. StrBuilder s = new StrBuilder();
  66. while (c != -1) {
  67. if (c == ' ' || c == '\n' || c == '\r')
  68. break;
  69. s.append((char) c);
  70. c = read();
  71. }
  72. return s.toStr();
  73. }
  74.  
  75. public char nextChar() throws Exception {
  76. int c;
  77. while ((c = read()) != -1 && (c == ' ' || c == '\n' || c == '\r'));
  78. return (char)c;
  79. }
  80.  
  81. public String nextLine() throws Exception {
  82. int c;
  83. while ((c = read()) != -1 && (c == ' ' || c == '\n' || c == '\r'))
  84. ;
  85. StrBuilder s = new StrBuilder();
  86. while (c != -1) {
  87. if (c == '\n' || c == '\r')
  88. break;
  89. s.append((char) c);
  90. c = read();
  91. }
  92. return s.toStr();
  93. }
  94.  
  95. public int nextInt() throws Exception {
  96. int ret = 0;
  97. byte c = read();
  98. while (c <= ' ')
  99. c = read();
  100. boolean neg = (c == '-');
  101. if (neg)
  102. c = read();
  103. do
  104. ret = ret * 10 + c - '0';
  105. while ((c = read()) >= '0' && c <= '9');
  106.  
  107. if (neg)
  108. return -ret;
  109. return ret;
  110. }
  111.  
  112. public int[] readIntArray(int n) throws Exception {
  113. int[] ar = new int[n];
  114. for (int i = 0; i < n; ++i)
  115. ar[i] = nextInt();
  116. return ar;
  117. }
  118.  
  119. public long nextLong() throws Exception {
  120. long ret = 0;
  121. byte c = read();
  122. while (c <= ' ')
  123. c = read();
  124. boolean neg = (c == '-');
  125. if (neg)
  126. c = read();
  127. do
  128. ret = ret * 10 + c - '0';
  129. while ((c = read()) >= '0' && c <= '9');
  130. if (neg)
  131. return -ret;
  132. return ret;
  133. }
  134.  
  135. public long[] readLongArray(int n) throws Exception {
  136. long[] ar = new long[n];
  137. for (int i = 0; i < n; ++i)
  138. ar[i] = nextLong();
  139. return ar;
  140. }
  141.  
  142. public double nextDouble() throws Exception {
  143. double ret = 0, div = 1;
  144. byte c = read();
  145. while (c <= ' ')
  146. c = read();
  147. boolean neg = (c == '-');
  148. if (neg)
  149. c = read();
  150. do
  151. ret = ret * 10 + c - '0';
  152. while ((c = read()) >= '0' && c <= '9');
  153. if (c == '.')
  154. while ((c = read()) >= '0' && c <= '9')
  155. ret += (c - '0') / (div *= 10);
  156. if (neg)
  157. return -ret;
  158. return ret;
  159. }
  160.  
  161. private void fillBuffer() throws Exception {
  162. bytesRead = System.in.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  163. if (bytesRead == -1)
  164. buffer[0] = -1;
  165. }
  166.  
  167. private byte read() throws Exception {
  168. if (bufferPointer == bytesRead)
  169. fillBuffer();
  170. return buffer[bufferPointer++];
  171. }
  172. }
  173.  
  174. static class StrBuilder {
  175. private char data[];
  176. int n, m;
  177. public StrBuilder() {
  178. data = new char[m = 4];
  179. n = 0;
  180. }
  181. public void append(char c) {
  182. if(n == m) resize(m *= 2);
  183. data[n++] = c;
  184. }
  185. public String toStr() {
  186. return new String(data, 0, n);
  187. }
  188. private void resize(int sz) {
  189. char tmp[] = new char[sz];
  190. for(int i = 0; i < n; ++i) tmp[i] = data[i];
  191. data = tmp;
  192. }
  193. }
  194. }
  195.  
Success #stdin #stdout 0.06s 34548KB
stdin
7
80967 41035 18400 18400 15320 14329 85
4
90 22222 25856 45875
stdout
6
3
3
2