結果

問題 No.1341 真ん中を入れ替えて門松列
ユーザー 37zigen37zigen
提出日時 2019-12-05 03:25:31
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,398 bytes
コンパイル時間 2,674 ms
コンパイル使用メモリ 82,032 KB
実行使用メモリ 65,004 KB
最終ジャッジ日時 2023-10-21 10:48:54
合計ジャッジ時間 11,670 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
57,864 KB
testcase_01 AC 124 ms
57,616 KB
testcase_02 WA -
testcase_03 AC 121 ms
57,592 KB
testcase_04 AC 122 ms
57,732 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 1,515 ms
63,784 KB
testcase_08 TLE -
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

//整理

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

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

	void run() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long M = sc.nextLong();
		long[][] A = new long[N][2];
		long[] C = new long[N];
		for (int i = 0; i < N; ++i) {
			A[i][0] = sc.nextInt();
			C[i] = sc.nextInt();
			A[i][1] = sc.nextInt();
		}
		String[] ret = solve(N, M, A, C);
		for (String s : ret) {
			System.out.println(s);
		}
	}

	long check(long middle, int N, long M, long[][] A, long[] C) {
		for (int i = 0; i < A.length; ++i)
			Arrays.sort(A[i]);
		Arrays.sort(C);
		for (int i = 0; i < N; ++i) {
			long l = binarySearchLeft(A[i][0], C);
			long r = binarySearchRight(A[i][1], C);
			l = Math.min(l, middle);
			r = Math.max(r, middle + 1);
			A[i] = new long[] { l, r, A[i][0], A[i][1] };
		}
		Arrays.sort(A, new Comparator<long[]>() {
			@Override
			public int compare(long[] o1, long[] o2) {
				return Long.compare(o1[0], o2[0]);
			}
		});
		PriorityQueue<long[]> pq = new PriorityQueue<>(new Comparator<long[]>() {
			@Override
			public int compare(long[] o1, long[] o2) {
				if (o1[0] != o2[0])
					return Long.compare(o1[0], o2[0]);
				else
					return Long.compare(o1[2], o2[2]);
			}
		});
		int p = 0, maxsz = 0;
		long ans = 0;
		int[] need = new int[N];
		int[] sz = new int[N];
		for (int i = -1; i <= N; ++i) {
			while (p < A.length && A[p][0] <= i) {
				pq.add(new long[] { A[p][1], A[p][2], A[p][3] });
				++p;
				maxsz = Math.max(maxsz, pq.size());
			}
			while (pq.size() > i + 1) {
				long[] v = pq.poll();
				if (v[0] >= N) {
					return -1;
				}
				need[(int) v[0]]--;
			}
			if (0 <= i && i < sz.length)
				sz[i] = pq.size();
		}
		for (int i = 1; i < N; ++i) {
			need[i] = need[i - 1] + need[i];
		}
		boolean ok = true;
		int pnd = 0, cur = 0;
		for (int i = 0; i < N; ++i) {
			pnd += Math.abs(need[i]);
			if (pnd > 0) {
				++cur;
				--pnd;
			}
			need[i] = (i + 1) - cur;
		}
		int max = 0;
		for (int i = N - 1; i >= 0; --i) {
			max = Math.max(max, sz[i]);
			ok &= max >= need[i];
		}
		if (!ok)
			return -1;
		for (int i = N - 1; i >= pq.size(); --i) {
			ans += C[i];
		}
		while (!pq.isEmpty()) {
			ans += pq.poll()[2];
		}
		return ans;
	}

	String[] solve(int N, long M, long[][] A, long[] C) {
		long ret = -1;
		for (int i = -1; i <= N; ++i) {
			long[][] Acopy = new long[A.length][A[0].length];
			for (int j = 0; j < A.length; ++j)
				Acopy[j] = Arrays.copyOf(A[j], A[j].length);
			ret = Math.max(ret, check(i, N, M, Acopy, Arrays.copyOf(C, C.length)));
			if (ret >= M)
				return new String[] { "YES", "KADOMATSU!" };
			else if (ret > 0) {
				return new String[] { "YES", "NO" };
			}
		}
		return new String[] { "NO" };
	}

	int binarySearchRight(long key, long[] a) {
		int ok = a.length;
		int ng = -1;
		while (ok - ng > 1) {
			int middle = (ok + ng) / 2;
			if (a[middle] > key) {
				ok = middle;
			} else {
				ng = middle;
			}
		}
		return ok;
	}

	int binarySearchLeft(long key, long[] a) {
		int ok = -1;
		int ng = a.length;
		while (ng - ok > 1) {
			int middle = (ok + ng) / 2;
			if (a[middle] < key) {
				ok = middle;
			} else {
				ng = middle;
			}
		}
		return ok;
	}

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

}
0