結果
| 問題 |
No.1341 真ん中を入れ替えて門松列
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-12-02 21:59:34 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,655 bytes |
| コンパイル時間 | 2,246 ms |
| コンパイル使用メモリ | 82,576 KB |
| 実行使用メモリ | 48,764 KB |
| 最終ジャッジ日時 | 2024-09-21 11:56:36 |
| 合計ジャッジ時間 | 7,491 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 4 WA * 10 |
ソースコード
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);
// 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 = 10;
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(5)), Math.abs(rnd.nextInt(5)), Math.abs(rnd.nextInt(5)) };
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);
if (solve(N, M, A, C)[1].equals("NO")) {
System.exit(-1);
}
}
String[] solve(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);
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, pnd = 0, need = 0, maxsz = 0;
long ans = 0;
int[] right = 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());
}
pnd += (0 <= i && i < right.length ? right[i] : 0);
if (0 <= i && i < N)
if (pnd == 0)
++need;
else {
--pnd;
}
while (pq.size() > i + 1) {
long[] v = pq.poll();
if (v[0] >= N) {
return new String[] { "NO" };
}
right[(int) v[0]]++;
}
}
String[] ret = new String[2];
ret[0] = (pq.size() == need ? "YES" : "NO");
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;
}
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));
}
}