|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + |
| 7 | + static class Node{ |
| 8 | + int point; |
| 9 | + Node left; |
| 10 | + Node right; |
| 11 | + boolean isAlive = true; |
| 12 | + |
| 13 | + public Node (int point){ |
| 14 | + this.point = point; |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + static class Edge implements Comparable<Edge>{ |
| 19 | + Node left; |
| 20 | + Node right; |
| 21 | + int distance; |
| 22 | + |
| 23 | + |
| 24 | + public Edge(Node left, Node right){ |
| 25 | + this.left = left; |
| 26 | + this.right = right; |
| 27 | + distance = right.point - left.point; |
| 28 | + |
| 29 | + left.right = right; |
| 30 | + right.left = left; |
| 31 | + } |
| 32 | + |
| 33 | + public boolean isAlive(){ |
| 34 | + if (left.isAlive && right.isAlive |
| 35 | + && left.right == right |
| 36 | + && right.left == left) return true; |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public int compareTo(Edge e){ |
| 42 | + if (e.distance == this.distance) { |
| 43 | + return Integer.compare(this.left.point , e.left.point); |
| 44 | + } |
| 45 | + return Integer.compare(e.distance, this.distance); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + static final int INIT = 100; |
| 50 | + static final int ADD = 200; |
| 51 | + static final int DELETE = 300; |
| 52 | + static final int CALCULATE = 400; |
| 53 | + |
| 54 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 55 | + static StringBuilder sb = new StringBuilder(); |
| 56 | + |
| 57 | + static int totalStreet,nodeCnt, orderCnt, aliveCnt, min,max; |
| 58 | + static List<Node> nodes = new ArrayList<>(); |
| 59 | + static PriorityQueue<Edge> pq = new PriorityQueue<>(); |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + public static void main(String[] args) throws IOException { |
| 64 | + init(); |
| 65 | + process(); |
| 66 | + print(); |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + private static void init() throws IOException { |
| 71 | + orderCnt = Integer.parseInt(br.readLine()); |
| 72 | + |
| 73 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 74 | + |
| 75 | + st.nextToken(); // 100 |
| 76 | + |
| 77 | + totalStreet = Integer.parseInt(st.nextToken()); |
| 78 | + nodeCnt = Integer.parseInt(st.nextToken()); |
| 79 | + aliveCnt = nodeCnt; |
| 80 | + min = totalStreet; |
| 81 | + max = 0; |
| 82 | + |
| 83 | + Node dummy = new Node(-1); |
| 84 | + nodes.add(dummy); |
| 85 | + for (int i = 1; i<= nodeCnt; i++){ |
| 86 | + int point = Integer.parseInt(st.nextToken()); |
| 87 | + min = Math.min(min, point); |
| 88 | + max = Math.max(max, point); |
| 89 | + Node n = new Node(point); |
| 90 | + nodes.add(n); |
| 91 | + } |
| 92 | + |
| 93 | + for (int i = 1; i < nodeCnt; i++){ |
| 94 | + Node left = nodes.get(i); |
| 95 | + Node right = nodes.get(i+1); |
| 96 | + Edge e = new Edge(left,right); |
| 97 | + pq.add(e); |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + private static void process() throws IOException { |
| 103 | + for (int i = 1 ; i < orderCnt; i++){ |
| 104 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 105 | + int order = Integer.parseInt(st.nextToken()); |
| 106 | + |
| 107 | + if (order == ADD){ |
| 108 | + add(); |
| 109 | + } else if (order == DELETE){ |
| 110 | + int target = Integer.parseInt(st.nextToken()); |
| 111 | + delete(target); |
| 112 | + } else if (order == CALCULATE){ |
| 113 | + calculate(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + private static void add(){ |
| 120 | + aliveCnt++; |
| 121 | + |
| 122 | + Edge e = pq.poll(); |
| 123 | + |
| 124 | + while(!e.isAlive()){ |
| 125 | + e = pq.poll(); |
| 126 | + } |
| 127 | + |
| 128 | + int nPoint = e.left.point + (e.distance + 1)/2; |
| 129 | + Node n = new Node(nPoint); |
| 130 | + nodes.add(n); |
| 131 | + Edge e1 = new Edge(e.left, n); |
| 132 | + Edge e2 = new Edge(n, e.right); |
| 133 | + |
| 134 | + pq.add(e1); |
| 135 | + pq.add(e2); |
| 136 | + } |
| 137 | + |
| 138 | + private static void delete(int target){ |
| 139 | + aliveCnt--; |
| 140 | + Node t = nodes.get(target); |
| 141 | + t.isAlive = false; |
| 142 | + |
| 143 | + if (t.left == null){ |
| 144 | + // 이 노드가 가장 왼쪽(시작지점)에 있는 경우 |
| 145 | + min = t.right.point; |
| 146 | + t.right.left = null; |
| 147 | + } else if (t.right == null){ |
| 148 | + // 이 노드가 가장 오른쪽(끝)에 있는 경우 |
| 149 | + max = t.left.point; |
| 150 | + t.left.right = null; |
| 151 | + } else{ |
| 152 | + Edge e = new Edge(t.left, t.right); |
| 153 | + pq.add(e); |
| 154 | + } |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + private static void calculate(){ |
| 162 | + Edge e = pq.peek(); |
| 163 | + |
| 164 | + while (!e.isAlive()){ |
| 165 | + pq.poll(); |
| 166 | + e = pq.peek(); |
| 167 | + } |
| 168 | + |
| 169 | + int cost = e.distance; |
| 170 | + |
| 171 | + cost = Math.max( cost,(min-1)*2); |
| 172 | + cost = Math.max( cost,(totalStreet - max)*2); |
| 173 | + |
| 174 | + sb.append(cost).append("\n"); |
| 175 | + |
| 176 | + } |
| 177 | + |
| 178 | + private static void print(){ |
| 179 | + System.out.println(sb.toString()); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +``` |
0 commit comments