|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class BJ_23090_난민 { |
| 6 | + |
| 7 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 8 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 9 | + private static final StringBuilder sb = new StringBuilder(); |
| 10 | + private static StringTokenizer st; |
| 11 | + |
| 12 | + private static int N, x, y, prevY; |
| 13 | + private static long totalDist; |
| 14 | + private static Shelter newShelter; |
| 15 | + private static Queue<Shelter> maxHeap = new PriorityQueue<>((a, b) -> Integer.compare(b.y, a.y)); |
| 16 | + private static Queue<Shelter> minHeap = new PriorityQueue<>((a, b) -> Integer.compare(a.y, b.y)); |
| 17 | + private static List<Shelter> shelters = new ArrayList<>(); |
| 18 | + |
| 19 | + private static class Shelter { |
| 20 | + int x; |
| 21 | + int y; |
| 22 | + |
| 23 | + Shelter(int x, int y) { |
| 24 | + this.x = x; |
| 25 | + this.y = y; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + public static void main(String[] args) throws IOException { |
| 30 | + N = Integer.parseInt(br.readLine()); |
| 31 | + |
| 32 | + while (N-- > 0) { |
| 33 | + init(); |
| 34 | + sol(); |
| 35 | + } |
| 36 | + |
| 37 | + bw.write(sb.toString()); |
| 38 | + bw.flush(); |
| 39 | + bw.close(); |
| 40 | + br.close(); |
| 41 | + } |
| 42 | + |
| 43 | + private static void init() throws IOException { |
| 44 | + st = new StringTokenizer(br.readLine()); |
| 45 | + x = Integer.parseInt(st.nextToken()); |
| 46 | + y = Integer.parseInt(st.nextToken()); |
| 47 | + |
| 48 | + newShelter = new Shelter(x, y); |
| 49 | + } |
| 50 | + |
| 51 | + private static void sol() { |
| 52 | + if (maxHeap.isEmpty() || newShelter.y <= maxHeap.peek().y) { |
| 53 | + maxHeap.add(newShelter); |
| 54 | + } else { |
| 55 | + minHeap.add(newShelter); |
| 56 | + } |
| 57 | + |
| 58 | + if (maxHeap.size() > minHeap.size() + 1) { |
| 59 | + minHeap.add(maxHeap.poll()); |
| 60 | + } else if (minHeap.size() > maxHeap.size()) { |
| 61 | + maxHeap.add(minHeap.poll()); |
| 62 | + } |
| 63 | + |
| 64 | + int curY = maxHeap.peek().y; |
| 65 | + |
| 66 | + // 새로 생긴 대피소의 x, y값 더하기 |
| 67 | + totalDist += Math.abs(newShelter.x); |
| 68 | + totalDist += Math.abs(newShelter.y - curY); |
| 69 | + |
| 70 | + // 변화된 만큼만 계산 |
| 71 | + int diff = curY - prevY; |
| 72 | + int lowerCount = maxHeap.size() - 1; |
| 73 | + int upperCount = minHeap.size(); |
| 74 | + |
| 75 | + totalDist += (long) (lowerCount - upperCount) * diff; |
| 76 | + |
| 77 | + prevY = curY; |
| 78 | + |
| 79 | + sb.append(curY).append(" ").append(totalDist).append("\n"); |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
0 commit comments