|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + static class Node{ |
| 7 | + int key,difficulty; |
| 8 | + |
| 9 | + Node(int key, int difficulty){ |
| 10 | + this.key = key; |
| 11 | + this.difficulty = difficulty; |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 16 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 17 | + static StringTokenizer st; |
| 18 | + static int N,M; |
| 19 | + static Map<Integer,Integer> problems = new HashMap<>(); |
| 20 | + static TreeSet<Node> s = new TreeSet<>((a,b) -> { |
| 21 | + if(a.difficulty == b.difficulty){ |
| 22 | + return Integer.compare(a.key,b.key); |
| 23 | + }else{ |
| 24 | + return Integer.compare(a.difficulty,b.difficulty); |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + public static void main(String[] args) throws IOException { |
| 29 | + N = Integer.parseInt(br.readLine()); |
| 30 | + for (int i = 0; i < N; i++) { |
| 31 | + st = new StringTokenizer(br.readLine()); |
| 32 | + int p = Integer.parseInt(st.nextToken()); |
| 33 | + int l = Integer.parseInt(st.nextToken()); |
| 34 | + problems.put(p,l); |
| 35 | + s.add(new Node(p,l)); |
| 36 | + } |
| 37 | + |
| 38 | + M = Integer.parseInt(br.readLine()); |
| 39 | + for (int i = 0; i < M; i++) { |
| 40 | + st = new StringTokenizer(br.readLine()); |
| 41 | + String cmd = st.nextToken(); |
| 42 | + |
| 43 | + if(cmd.equals("add")){ |
| 44 | + int p = Integer.parseInt(st.nextToken()); |
| 45 | + int l = Integer.parseInt(st.nextToken()); |
| 46 | + problems.put(p,l); |
| 47 | + s.add(new Node(p,l)); |
| 48 | + } |
| 49 | + else if(cmd.equals("recommend")){ |
| 50 | + int x = Integer.parseInt(st.nextToken()); |
| 51 | + if(x == 1){ |
| 52 | + Node hard = s.last(); |
| 53 | + bw.write(hard.key + "\n"); |
| 54 | + } |
| 55 | + else{ |
| 56 | + Node easy = s.first(); |
| 57 | + bw.write(easy.key + "\n"); |
| 58 | + } |
| 59 | + } |
| 60 | + else if(cmd.equals("solved")){ |
| 61 | + int p = Integer.parseInt(st.nextToken()); |
| 62 | + int difficulty = problems.get(p); |
| 63 | + problems.remove(p); |
| 64 | + s.remove(new Node(p, difficulty)); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + bw.close(); |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
0 commit comments