|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.HashSet; |
| 8 | +import java.util.List; |
| 9 | +import java.util.StringTokenizer; |
| 10 | + |
| 11 | +public class Main { |
| 12 | + |
| 13 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 14 | + static StringBuilder sb = new StringBuilder(); |
| 15 | + static int partyCnt,day; |
| 16 | + static HashSet<Party> set; |
| 17 | + |
| 18 | + static class Party{ |
| 19 | + int start, end,idx; |
| 20 | + |
| 21 | + public Party(int start, int end, int idx) { |
| 22 | + this.idx = idx; |
| 23 | + this.start = start; |
| 24 | + this.end = end; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + public static void main(String[] args) throws IOException { |
| 29 | + day = 0; |
| 30 | + while(true) { |
| 31 | + partyCnt = Integer.parseInt(br.readLine()); |
| 32 | + if (partyCnt == 0 ) break; |
| 33 | + init(); |
| 34 | + process(); |
| 35 | + } |
| 36 | + print(); |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + private static void init() throws IOException{ |
| 41 | + day++; |
| 42 | + set = new HashSet<>(); |
| 43 | + for (int i = 0; i < partyCnt; i++) { |
| 44 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 45 | + int start = Integer.parseInt(st.nextToken()); |
| 46 | + int end = Integer.parseInt(st.nextToken()); |
| 47 | + set.add(new Party(start, end,i)); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private static void process() throws IOException { |
| 52 | + // 23시부터 시작, 제일 늦게 끝나는것부터 찾아서 넣기. |
| 53 | + int cnt = 0; |
| 54 | + |
| 55 | + for (int i = 23; i >= 8; i--) { |
| 56 | + |
| 57 | + for (int j = 0; j < 2; j++) { |
| 58 | + int maxDay = 0; |
| 59 | + Party target = null; |
| 60 | + for (Party p: set) { |
| 61 | + if (p.end > i && p.start <= i ) { |
| 62 | + if (maxDay < p.start) { |
| 63 | + maxDay = p.start; |
| 64 | + target = p; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (target != null) { |
| 70 | + set.remove(target); |
| 71 | + cnt++; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + String ans = String.format("On day %d Emma can attend as many as %d parties.\n",day,cnt); |
| 77 | + sb.append(ans); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + private static void print() { |
| 82 | + System.out.print(sb.toString()); |
| 83 | + } |
| 84 | + |
| 85 | +} |
| 86 | +``` |
0 commit comments