結果

問題 No.1341 真ん中を入れ替えて門松列
ユーザー 37zigen37zigen
提出日時 2019-12-05 02:57:25
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,810 bytes
コンパイル時間 3,195 ms
コンパイル使用メモリ 92,296 KB
実行使用メモリ 49,496 KB
最終ジャッジ日時 2024-09-21 11:55:49
合計ジャッジ時間 10,254 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
47,212 KB
testcase_01 AC 120 ms
41,672 KB
testcase_02 AC 105 ms
40,284 KB
testcase_03 AC 117 ms
41,188 KB
testcase_04 AC 117 ms
41,228 KB
testcase_05 AC 118 ms
41,708 KB
testcase_06 AC 261 ms
46,796 KB
testcase_07 AC 1,553 ms
48,728 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.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		new Main().run();
		// for (int i = 0; i < 10000; ++i) {
		// System.out.println("i=" + i);
		// new Main().random();
		// }
	}

	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);
		}
	}

	void random() {
		Random rnd = new Random();
		int N = 100;
		long M = 0;
		long[][] A = new long[N][2];
		long[] C = new long[N];
		List<Long> list = new ArrayList<>();
		for (int i = 0; i < N; ++i) {
			long[] x = new long[] { 1, 1, 1 };
			while (x[0] == x[1] || x[1] == x[2]) {
				x = new long[] { Math.abs(rnd.nextInt(100)), Math.abs(rnd.nextInt(100)), Math.abs(rnd.nextInt(100)) };
				Arrays.sort(x);
			}
			M += x[2];
			if (rnd.nextInt() % 2 == 1) {
				A[i][0] = x[0];
				list.add(x[2]);
				A[i][1] = x[1];
			} else {
				A[i][0] = x[1];
				list.add(x[0]);
				A[i][1] = x[2];
			}
			Arrays.sort(A[i]);
		}
		Collections.shuffle(list);
		for (int i = 0; i < N; ++i)
			C[i] = list.get(i);
		System.out.println(N + " " + M);
		for (int i = 0; i < N; ++i) {
			System.out.println(A[i][0] + " " + C[i] + " " + A[i][1]);
		}
		String[] ret = solve(N, M, A, C);
		if (ret[0].equals("NO")) {
			throw new AssertionError();
		}
	}

	String[] 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 new String[] { "NO" };
				}
				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 new String[] { "NO" };
		String[] ret = new String[2];
		ret[0] = "YES";
		for (int i = N - 1; i >= pq.size(); --i) {
			ans += C[i];
		}
		while (!pq.isEmpty()) {
			ans += pq.poll()[2];
		}
		if (ans >= M) {
			ret[1] = "KADOMATSU!";
		} else {
			ret[1] = "NO";
		}
		return ret;
	}
	// 1 3 2
	// 2 1 4

	String[] solve(int N, long M, long[][] A, long[] C) {
		boolean YES = false;
		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);
			String[] ret = (check(i, N, M, Acopy, Arrays.copyOf(C, C.length)));
			if (ret[0].equals("YES") && ret[1].equals("KADOMATSU!"))
				return ret;
			else if (ret[0].equals("YES")) {
				YES = true;
			}
		}
		return YES ? new String[] { "YES", "NO" } : 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