結果

問題 No.580 旅館の予約計画
ユーザー 37zigen
提出日時 2017-10-22 23:55:11
言語 Java
(openjdk 23)
結果
AC  
実行時間 248 ms / 2,000 ms
コード長 1,884 bytes
コンパイル時間 2,693 ms
コンパイル使用メモリ 82,744 KB
実行使用メモリ 46,396 KB
最終ジャッジ日時 2024-11-21 14:37:12
合計ジャッジ時間 10,296 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

class Solution {
	public static void main(String[] args) {
		new Solution().run();
	}

	class Edge implements Comparable<Edge> {
		int src;
		int dst;

		public Edge(int src, int dst) {
			this.src = src;
			this.dst = dst;
		}

		@Override
		public int compareTo(Edge o) {
			return Integer.compare(src, o.src);
		}
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int[][] v = new int[m][2];
		for (int i = 0; i < m; ++i) {
			int v1 = sc.nextInt();
			String[] s1 = sc.next().split(":");
			int v2 = sc.nextInt();
			String[] s2 = sc.next().split(":");
			int src = v1 * 10000 + new Integer(s1[0]) * 100 + new Integer(s1[1]);
			int dst = v2 * 10000 + new Integer(s2[0]) * 100 + new Integer(s2[1]);
			v[i][0] = src;
			v[i][1] = dst;
		}

		Arrays.sort(v, new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				return Integer.compare(o1[0], o2[0]);
			}
		});
		int ans = 0;
		int cnt = 0;
		ArrayList<Edge> pending = new ArrayList<>();
		for (int i = 0; i < m; ++i) {
			int src = v[i][0];
			int dst = v[i][1];
			pending.add(new Edge(v[i][0], v[i][1]));
			++cnt;
			if (cnt <= n) {
				continue;
			} else {
				Edge tmp = null;
				for (int j = 0; j < pending.size(); ++j) {
					if (pending.get(j).dst < src) {
						pending.remove(j);
						--j;
						--cnt;
						++ans;
					}
					if (j >= 0 && (tmp == null || tmp.dst < pending.get(j).dst)) {
						tmp = pending.get(j);
					}
				}
				if (cnt > n) {
					pending.remove(tmp);
					--cnt;
					if (cnt > n)
						throw new AssertionError();
				}
			}
		}
		ans += cnt;
		System.out.println(ans);
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}
0