fork(1) download
  1. import java.io.DataInputStream;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.util.Comparator;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import java.util.PriorityQueue;
  8.  
  9. public class Main {
  10.  
  11. static class Reader {
  12.  
  13. final private int BUFFER_SIZE = 1 << 16;
  14.  
  15. private DataInputStream din;
  16.  
  17. private byte[] buffer;
  18.  
  19. private int bufferPointer, bytesRead;
  20.  
  21. public Reader() {
  22.  
  23. din = new DataInputStream(System.in);
  24.  
  25. buffer = new byte[BUFFER_SIZE];
  26.  
  27. bufferPointer = bytesRead = 0;
  28.  
  29. }
  30.  
  31. public Reader(String file_name) throws IOException {
  32.  
  33. din = new DataInputStream(new FileInputStream(file_name));
  34.  
  35. buffer = new byte[BUFFER_SIZE];
  36.  
  37. bufferPointer = bytesRead = 0;
  38.  
  39. }
  40.  
  41. public String readLine() throws IOException {
  42.  
  43. byte[] buf = new byte[64]; // line length
  44.  
  45. int cnt = 0, c;
  46.  
  47. while ((c = read()) != -1) {
  48.  
  49. if (c == '\n') {
  50.  
  51. break;
  52.  
  53. }
  54.  
  55. buf[cnt++] = (byte) c;
  56.  
  57. }
  58.  
  59. return new String(buf, 0, cnt);
  60.  
  61. }
  62.  
  63. public int nextInt() throws IOException {
  64.  
  65. int ret = 0;
  66.  
  67. byte c = read();
  68.  
  69. while (c <= ' ') {
  70.  
  71. c = read();
  72.  
  73. }
  74.  
  75. boolean neg = (c == '-');
  76.  
  77. if (neg) {
  78.  
  79. c = read();
  80.  
  81. }
  82.  
  83. do {
  84.  
  85. ret = ret * 10 + c - '0';
  86.  
  87. } while ((c = read()) >= '0' && c <= '9');
  88.  
  89. if (neg) {
  90.  
  91. return -ret;
  92.  
  93. }
  94.  
  95. return ret;
  96.  
  97. }
  98.  
  99. private void fillBuffer() throws IOException {
  100.  
  101. bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  102.  
  103. if (bytesRead == -1) {
  104.  
  105. buffer[0] = -1;
  106.  
  107. }
  108.  
  109. }
  110.  
  111. private byte read() throws IOException {
  112.  
  113. if (bufferPointer == bytesRead) {
  114.  
  115. fillBuffer();
  116.  
  117. }
  118.  
  119. return buffer[bufferPointer++];
  120.  
  121. }
  122.  
  123. public void close() throws IOException {
  124.  
  125. if (din == null) {
  126.  
  127. return;
  128.  
  129. }
  130.  
  131. din.close();
  132.  
  133. }
  134.  
  135. }
  136. public static void main(String[] args){// throws IOException {
  137. Map<String,Integer> mp=new HashMap<>();
  138. PriorityQueue<String> pq=new PriorityQueue<>(new Comparator<String>(){
  139. @Override
  140. public int compare(String t, String t1) {
  141. return mp.get(t)-mp.get(t1);
  142. }
  143. });
  144. Reader sc=new Reader();
  145. int n=-1;
  146. try
  147. {
  148. n=Integer.parseInt(sc.readLine());
  149. sc.readLine();
  150. }
  151. catch(Exception e){
  152. return;
  153. }
  154. int x=1;
  155. StringBuilder sba=new StringBuilder("");
  156. while(x<=n){
  157. String []h=null;
  158. try
  159. {
  160. h=sc.readLine().split(" ");
  161. sc.readLine();
  162. }
  163. catch(Exception e){
  164. return ;
  165. }
  166. // System.out.print("string for this input is ");
  167. // for (int i = 0; i < h.length; i++) {
  168. // System.out.print(h[i]+" , ");
  169. // }
  170. // System.out.println("");
  171. int type=Integer.parseInt(h[0]);
  172. if(type!=3){
  173. int newVal=Integer.parseInt(h[2]);
  174. mp.put(h[1].intern(),newVal);
  175. if(!pq.isEmpty()){
  176. while(!pq.isEmpty() && pq.remove(h[1].intern()))
  177. pq.remove(h[1].intern());
  178. }
  179. pq.offer(h[1].intern());
  180. }
  181. else{
  182. String j=pq.poll();
  183. while(!pq.isEmpty() && pq.remove(j.intern()))
  184. pq.remove(j.intern());
  185. sba.append(j+" "+x).append("\n\n");
  186. }
  187. x+=1;
  188. // System.out.println("x= "+x);
  189. }
  190. System.out.println(sba.toString());
  191. }
  192.  
  193. }
Success #stdin #stdout 0.05s 2184192KB
stdin
Standard input is empty
stdout
Standard output is empty