結果

問題 No.580 旅館の予約計画
ユーザー tentententen
提出日時 2020-09-07 20:20:27
言語 Java21
(openjdk 21)
結果
AC  
実行時間 230 ms / 2,000 ms
コード長 2,485 bytes
コンパイル時間 2,678 ms
コンパイル使用メモリ 79,784 KB
実行使用メモリ 46,240 KB
最終ジャッジ日時 2024-05-07 00:06:32
合計ジャッジ時間 9,585 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 110 ms
41,088 KB
testcase_01 AC 116 ms
41,116 KB
testcase_02 AC 117 ms
41,288 KB
testcase_03 AC 119 ms
41,388 KB
testcase_04 AC 126 ms
41,472 KB
testcase_05 AC 130 ms
41,356 KB
testcase_06 AC 161 ms
41,792 KB
testcase_07 AC 135 ms
41,708 KB
testcase_08 AC 145 ms
41,376 KB
testcase_09 AC 158 ms
42,148 KB
testcase_10 AC 161 ms
42,264 KB
testcase_11 AC 163 ms
41,748 KB
testcase_12 AC 184 ms
43,020 KB
testcase_13 AC 165 ms
42,284 KB
testcase_14 AC 188 ms
43,560 KB
testcase_15 AC 224 ms
45,812 KB
testcase_16 AC 217 ms
45,504 KB
testcase_17 AC 230 ms
45,948 KB
testcase_18 AC 218 ms
45,908 KB
testcase_19 AC 211 ms
45,164 KB
testcase_20 AC 209 ms
46,032 KB
testcase_21 AC 217 ms
46,240 KB
testcase_22 AC 208 ms
45,412 KB
testcase_23 AC 207 ms
45,292 KB
testcase_24 AC 214 ms
45,904 KB
testcase_25 AC 201 ms
45,792 KB
testcase_26 AC 127 ms
41,580 KB
testcase_27 AC 130 ms
41,664 KB
testcase_28 AC 133 ms
41,076 KB
testcase_29 AC 136 ms
41,656 KB
testcase_30 AC 223 ms
45,532 KB
testcase_31 AC 215 ms
45,696 KB
testcase_32 AC 213 ms
45,888 KB
testcase_33 AC 204 ms
45,300 KB
testcase_34 AC 220 ms
45,584 KB
testcase_35 AC 120 ms
40,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
    	Scanner sc = new Scanner(System.in);
    	int n = sc.nextInt();
    	int m = sc.nextInt();
    	PriorityQueue<Reserve> queue = new PriorityQueue<>();
    	for (int i = 0; i < m; i++) {
    	    queue.add(new Reserve(i, sc.nextInt(), sc.next(), sc.nextInt(), sc.next()));
    	}
    	int outBound = 0;
    	TreeSet<Remain> remains = new TreeSet<>();
    	while (queue.size() > 0) {
    	    Reserve r = queue.poll();
    	    while (remains.size() > 0 && remains.first().time < r.start) {
    	        remains.remove(remains.pollFirst());
    	    }
    	    remains.add(new Remain(r));
    	    while (remains.size() > n) {
    	        remains.pollLast();
    	        outBound++;
    	    }
    	}
    	System.out.println(m - outBound);
    }
    
    static class Remain implements Comparable<Remain> {
        int idx;
        int time;
        
        public Remain(int idx, int time) {
            this.idx = idx;
            this.time = time;
        }
        
        public Remain(Reserve r) {
            this(r.idx, r.end);
        }
        
        public int hashCode() {
            return idx;
        }
        
        public int compareTo(Remain another) {
            if (time == another.time) {
                return idx - another.idx;
            } else {
                return time - another.time;
            }
        }
        
        public boolean equals(Object o) {
            Remain r = (Remain) o;
            return idx == r.idx;
        }
    }
    
    static class Reserve implements Comparable<Reserve> {
        int idx;
        int start;
        int end;
        
        public Reserve(int idx, int start, int end) {
            this.idx = idx;
            this.start = start;
            this.end = end;
        }
        
        public Reserve(int idx, int startDay, String startTime, int endDay, String endTime) {
            this(idx, getMinute(startDay, startTime), getMinute(endDay, endTime));
        }
        
        static int getMinute(int day, String time) {
            return (day - 2) * 24 * 60 + timeToMinute(time);
        }
        
        static int timeToMinute(String time) {
            String[] arr = time.split(":", 2);
            return Integer.parseInt(arr[0]) * 60 + Integer.parseInt(arr[1]);
        }
        
        public int compareTo(Reserve another) {
            return start - another.start;
        }
    }
}
0