fork(20) download
  1. import java.io.OutputStreamWriter;
  2. import java.io.BufferedWriter;
  3. import java.util.Comparator;
  4. import java.io.OutputStream;
  5. import java.io.PrintWriter;
  6. import java.util.RandomAccess;
  7. import java.util.AbstractList;
  8. import java.io.Writer;
  9. import java.util.List;
  10. import java.io.IOException;
  11. import java.util.Arrays;
  12. import java.util.InputMismatchException;
  13. import java.math.BigInteger;
  14. import java.io.InputStream;
  15. import java.util.ArrayList;
  16. import java.util.Collection;
  17. import java.util.HashMap;
  18. import java.util.Iterator;
  19. import java.util.Set;
  20.  
  21. public class Main
  22. {
  23.  
  24. public static void main(String[] args)
  25. {
  26. InputStream inputStream = System.in;
  27. OutputStream outputStream = System.out;
  28. InputReader in = new InputReader(inputStream);
  29. OutputWriter out = new OutputWriter(outputStream);
  30. BoyExercise solver = new BoyExercise();
  31. int T = 1;
  32. for(int count = 0; count < T; count++)
  33. {
  34. solver.solve(count+1, in, out);
  35. }
  36. out.close();
  37.  
  38. }//end of main()
  39. }
  40.  
  41. class BoyExercise
  42. {
  43. int N;
  44. String book[];
  45. int numberOfEx[];
  46. int min[];
  47. int currIndex;
  48.  
  49. public void solve(int testNumber, InputReader in, OutputWriter out)
  50. {
  51. N = in.readInt();
  52. book = new String[N + 1];
  53. numberOfEx = new int[N + 1];
  54. min = new int[N + 1];
  55. currIndex = 0;
  56.  
  57. min[0] = 0;
  58. numberOfEx[0] = Integer.MAX_VALUE;
  59. book[0] = "invalid";
  60.  
  61. for(int i = 1; i <= N; i++)
  62. {
  63. int numOfExercise = in.readInt();
  64.  
  65. if(numOfExercise == -1)
  66. {
  67. readBook(out);
  68. }
  69. else
  70. {
  71. if(numOfExercise == 0)
  72. {
  73. in.readString();
  74. continue;
  75. }
  76.  
  77. currIndex++;
  78. book[currIndex] = in.readString();
  79. numberOfEx[currIndex] = numOfExercise;
  80.  
  81. if(numOfExercise <= numberOfEx[currIndex - 1])
  82. {
  83. min[currIndex] = currIndex;
  84. }
  85. else
  86. {
  87. min[currIndex] = min[currIndex - 1];
  88. }
  89. //out.print("currIndex:"+currIndex+", book["+currIndex+"] = "+book[currIndex]+" and numberOfEx["+currIndex+"] = "+numberOfEx[currIndex] + "and min["+currIndex+"] = " + min[currIndex] + "\n");
  90. }
  91. }
  92.  
  93. //out.print(ans+"\n");
  94.  
  95. }//end of solve()
  96.  
  97. public void readBook(OutputWriter out)
  98. {
  99. for(int count = 0; count < currIndex; count++)
  100. {
  101. //out.print(min[count + 1] + "::" + book[count + 1] + "\n");
  102. }
  103.  
  104. int ansA = currIndex - min[currIndex];
  105. String ansB = book[min[currIndex]];
  106.  
  107. out.print(ansA + " " + ansB + "\n");
  108. currIndex = min[currIndex] - 1;
  109. }
  110.  
  111. }//end of class BoyExercise
  112.  
  113. class InputReader
  114. {
  115.  
  116. private InputStream stream;
  117. private byte[] buf = new byte[1024];
  118. private int curChar;
  119. private int numChars;
  120.  
  121. public InputReader(InputStream stream)
  122. {
  123. this.stream = stream;
  124. }
  125.  
  126. public int read()
  127. {
  128. if (numChars == -1)
  129. throw new InputMismatchException();
  130. if (curChar >= numChars)
  131. {
  132. curChar = 0;
  133. try
  134. {
  135. numChars = stream.read(buf);
  136. }
  137. catch (IOException e)
  138. {
  139. throw new InputMismatchException();
  140. }
  141.  
  142. if (numChars <= 0)
  143. return -1;
  144. }
  145. return buf[curChar++];
  146. }
  147.  
  148. public int readInt()
  149. {
  150. int c = read();
  151.  
  152. while (isSpaceChar(c))
  153. c = read();
  154.  
  155. int sgn = 1;
  156. if (c == '-')
  157. {
  158. sgn = -1;
  159. c = read();
  160. }
  161.  
  162. int res = 0;
  163. do
  164. {
  165. if (c < '0' || c > '9')
  166. throw new InputMismatchException();
  167. res *= 10;
  168. res += c - '0';
  169. c = read();
  170. }
  171. while (!isSpaceChar(c));
  172.  
  173. return res * sgn;
  174. }
  175.  
  176. public String readString()
  177. {
  178. StringBuilder sb = new StringBuilder();
  179. int c = read();
  180. while (isWhiteSpace(c))
  181. {
  182. c = read();
  183. }
  184.  
  185. while (!isWhiteSpace(c))
  186. {
  187. sb.appendCodePoint(c);
  188. c = read();
  189. }
  190.  
  191. return sb.toString();
  192. }
  193.  
  194. public static boolean isWhiteSpace(int c)
  195. {
  196. return c >= -1 && c <= 32;
  197. }
  198.  
  199. public static boolean isSpaceChar(int c)
  200. {
  201. return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  202. }
  203.  
  204. }
  205.  
  206. class OutputWriter
  207. {
  208. private final PrintWriter writer;
  209.  
  210. public OutputWriter(OutputStream outputStream)
  211. {
  212. writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
  213. }
  214.  
  215. public OutputWriter(Writer writer)
  216. {
  217. this.writer = new PrintWriter(writer);
  218. }
  219.  
  220. public void print(Object...objects)
  221. {
  222. for (int i = 0; i < objects.length; i++)
  223. {
  224. if (i != 0)
  225. writer.print(' ');
  226. writer.print(objects[i]);
  227. }
  228. }
  229.  
  230. public void printLine(Object...objects)
  231. {
  232. print(objects);
  233. writer.println();
  234. }
  235.  
  236. public void close()
  237. {
  238. writer.close();
  239. }
  240.  
  241. }
Success #stdin #stdout 0.03s 245760KB
stdin
6
1 a
2 b
0 c
1 d
-1
-1
stdout
0 d
1 a