|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.ArrayDeque; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | +import java.util.PriorityQueue; |
| 7 | +import java.util.Queue; |
| 8 | +import java.util.StringTokenizer; |
| 9 | +public class Main { |
| 10 | + public static void main(String[] args) throws IOException { |
| 11 | + Queue<Person> queue = new PriorityQueue<>(); |
| 12 | + |
| 13 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 14 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 15 | + int N = Integer.parseInt(st.nextToken()); |
| 16 | + int M = Integer.parseInt(st.nextToken()); |
| 17 | + int K = Integer.parseInt(st.nextToken()); |
| 18 | + |
| 19 | + |
| 20 | + List<Queue<Person>> lists = new ArrayList<>(); |
| 21 | + for (int i = 0; i < M; i++) { |
| 22 | + lists.add(new ArrayDeque<>()); |
| 23 | + } |
| 24 | + |
| 25 | + for (int i = 0; i < N; i++) { |
| 26 | + lists.get(i%M).add(Person.create(br, K, i, i%M)); |
| 27 | + } |
| 28 | + |
| 29 | + int answer = 0; |
| 30 | + for (int i = 0; i < M; i++) { |
| 31 | + if(!lists.get(i).isEmpty()) queue.add(lists.get(i).poll()); |
| 32 | + } |
| 33 | + while(true) { |
| 34 | + Person person = queue.poll(); |
| 35 | + if (person.getIsDeka()) { |
| 36 | + System.out.println(answer); |
| 37 | + break; |
| 38 | + } |
| 39 | + answer++; |
| 40 | + |
| 41 | + if(!lists.get(person.getLine()).isEmpty()) { |
| 42 | + queue.add(lists.get(person.getLine()).poll()); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + static class Person implements Comparable<Person> { |
| 50 | + private final int d; |
| 51 | + private final int h; |
| 52 | + private final boolean isDeka; |
| 53 | + private final int line; |
| 54 | + |
| 55 | + private Person(int d, int h, boolean isDeka, int line) { |
| 56 | + this.d = d; |
| 57 | + this.h = h; |
| 58 | + this.isDeka = isDeka; |
| 59 | + this.line = line; |
| 60 | + } |
| 61 | + |
| 62 | + public static Person create(BufferedReader br, int k, int index, int line) throws IOException { |
| 63 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 64 | + return new Person(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), index == k, line); |
| 65 | + } |
| 66 | + |
| 67 | + public int getLine() { |
| 68 | + return this.line; |
| 69 | + } |
| 70 | + |
| 71 | + public boolean getIsDeka() { |
| 72 | + return this.isDeka; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public int compareTo(Person o) { |
| 77 | + int compareD = o.d - this.d; |
| 78 | + if (compareD != 0) return compareD; |
| 79 | + |
| 80 | + int compareH = o.h - this.h; |
| 81 | + if (compareH != 0) return compareH; |
| 82 | + |
| 83 | + return this.line - o.line; |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +``` |
0 commit comments