結果

問題 No.580 旅館の予約計画
ユーザー tenten
提出日時 2020-09-07 20:20:27
言語 Java
(openjdk 23)
結果
AC  
実行時間 243 ms / 2,000 ms
コード長 2,485 bytes
コンパイル時間 2,859 ms
コンパイル使用メモリ 79,428 KB
実行使用メモリ 46,152 KB
最終ジャッジ日時 2024-11-29 11:16:58
合計ジャッジ時間 11,822 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

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