結果

問題 No.2957 Combo Deck Builder
ユーザー ks2m
提出日時 2024-11-08 22:28:31
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 1,711 bytes
コンパイル時間 2,713 ms
コンパイル使用メモリ 81,660 KB
実行使用メモリ 79,820 KB
最終ジャッジ日時 2024-11-08 22:29:14
合計ジャッジ時間 40,671 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 10 TLE * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.TreeSet;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		PriorityQueue<Obj> que1 = new PriorityQueue<>((o1, o2) -> o1.v - o2.v);
		PriorityQueue<Obj> que2 = new PriorityQueue<>((o1, o2) -> o2.v - o1.v);
		for (int i = 0; i < n; i++) {
			String[] sa = br.readLine().split(" ");
			Obj o = new Obj();
			o.c = Integer.parseInt(sa[0]);
			o.x = Integer.parseInt(sa[1]);
			o.y = Integer.parseInt(sa[2]);
			o.v = o.x - o.y;
			if (o.v < 0) {
				que1.add(o);
			} else {
				que2.add(o);
			}
		}
		br.close();

		TreeSet<Integer> set = new TreeSet<>();
		for (int i = 0; i < n; i++) {
			set.add(i);
		}
		Obj[] arr = new Obj[n];
		while (!que1.isEmpty()) {
			Obj o = que1.poll();
			Integer k = set.lower(o.c);
			if (k == null) {
				k = set.last();
			}
			arr[k] = o;
			set.remove(k);
		}

		int l = 0;
		for (int r = 0; r < n; r++) {
			if (arr[r] != null) {
				if (l < r) {
					arr[l] = arr[r];
					arr[r] = null;
				}
				l++;
			}
		}

		set.clear();
		for (int i = n - 1; i >= 0; i--) {
			if (arr[i] != null) {
				break;
			}
			set.add(i);
		}

		while (!que2.isEmpty()) {
			Obj o = que2.poll();
			Integer k = set.ceiling(o.c);
			if (k == null) {
				k = set.first();
			}
			arr[k] = o;
			set.remove(k);
		}

		long ans = 0;
		for (int i = 0; i < n; i++) {
			Obj o = arr[i];
			if (o.c <= i) {
				ans += o.x;
			} else {
				ans += o.y;
			}
		}
		System.out.println(ans);
	}

	static class Obj {
		int c, x, y, v;
	}
}
0