結果

問題 No.705 ゴミ拾い Hard
ユーザー 37zigen37zigen
提出日時 2018-07-01 04:18:11
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 2,071 bytes
コンパイル時間 2,501 ms
コンパイル使用メモリ 75,988 KB
実行使用メモリ 80,168 KB
最終ジャッジ日時 2023-09-13 16:30:12
合計ジャッジ時間 34,704 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
39,616 KB
testcase_01 AC 114 ms
39,180 KB
testcase_02 AC 115 ms
39,312 KB
testcase_03 AC 121 ms
40,260 KB
testcase_04 AC 116 ms
39,228 KB
testcase_05 AC 117 ms
39,104 KB
testcase_06 AC 119 ms
39,320 KB
testcase_07 AC 119 ms
39,080 KB
testcase_08 RE -
testcase_09 AC 119 ms
39,572 KB
testcase_10 AC 119 ms
39,012 KB
testcase_11 AC 120 ms
39,944 KB
testcase_12 AC 118 ms
39,236 KB
testcase_13 RE -
testcase_14 AC 197 ms
42,240 KB
testcase_15 AC 198 ms
42,488 KB
testcase_16 AC 206 ms
42,028 KB
testcase_17 AC 204 ms
42,496 KB
testcase_18 RE -
testcase_19 AC 201 ms
42,248 KB
testcase_20 WA -
testcase_21 AC 200 ms
42,120 KB
testcase_22 AC 204 ms
42,452 KB
testcase_23 AC 200 ms
42,064 KB
testcase_24 AC 1,381 ms
63,544 KB
testcase_25 AC 1,410 ms
63,056 KB
testcase_26 AC 1,405 ms
63,036 KB
testcase_27 AC 1,466 ms
63,408 KB
testcase_28 AC 1,430 ms
62,960 KB
testcase_29 AC 1,435 ms
62,696 KB
testcase_30 RE -
testcase_31 AC 1,431 ms
63,456 KB
testcase_32 AC 1,440 ms
62,840 KB
testcase_33 AC 1,093 ms
63,848 KB
testcase_34 AC 1,114 ms
62,932 KB
testcase_35 AC 1,337 ms
63,060 KB
testcase_36 AC 1,353 ms
62,716 KB
testcase_37 AC 1,215 ms
62,760 KB
testcase_38 AC 1,291 ms
62,976 KB
testcase_39 RE -
testcase_40 AC 119 ms
39,476 KB
testcase_41 AC 118 ms
39,108 KB
testcase_42 AC 118 ms
39,136 KB
testcase_43 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Scanner;

class Main {

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

	int N;
	long[] A, X, Y;
	long[] D;

	void run() {
		Scanner sc = new Scanner(System.in);
		N = sc.nextInt();
		A = new long[N];
		X = new long[N];
		Y = new long[N];
		D = new long[N];
		Arrays.fill(D, Long.MAX_VALUE / 3);
		for (int i = 0; i < N; ++i) {
			A[i] = sc.nextLong();
		}
		for (int i = 0; i < N; ++i) {
			X[i] = sc.nextLong();
		}
		for (int i = 0; i < N; ++i) {
			Y[i] = sc.nextLong();
		}
		// d[i]=min_{j<=i}(d[j-1]+cost[j,i])
		ArrayDeque<P> dq = new ArrayDeque<>();
		dq.add(new P(0, 0));
		for (int i = 0; i < N; ++i) {
			// d[i]を求める
			while (dq.size() >= 2 && second(dq).i <= i) {
				dq.pollFirst();
			}
			if (dq.size() > 0 && f(i, dq.peekFirst().j) >= f(i, i)) {
				dq.pollLast();
			}
			if (dq.isEmpty()) {
				dq.addLast(new P(i, i));
				D[i] = f(i, i);
				continue;
			}
			D[i] = f(i, dq.peekFirst().j);
			if (i == 0)
				continue;
			while (true) {
				int ok = N;
				int ng = i - 1;
				while (ok - ng > 1) {
					int m = (ok + ng) / 2;
					if (f(m, i) <= f(m, dq.peekLast().j)) {
						ok = m;
					} else {
						ng = m;
					}
				}
				if (ok > dq.peekLast().i) {
					if (ok < N)
						dq.addLast(new P(ok, i));
					break;
				} else {
					dq.pollLast();
				}
			}
		}
		System.out.println(D[N - 1]);
	}

	P second(ArrayDeque<P> dq) {
		if (dq.size() < 2)
			throw new AssertionError();
		P tmp = dq.pollFirst();
		P ret = new P(dq.peekFirst().i, dq.peekFirst().j);
		dq.addFirst(tmp);
		return ret;
	}

	long cost(int from, int to) {
		long dx = Math.abs(X[from] - A[to]);
		long dy = Math.abs(Y[from]);
		return dx * dx * dx + dy * dy * dy;
	}

	long f(int i, int j) {
		if (j > 0 && D[j - 1] == -1)
			throw new AssertionError();
		return (j > 0 ? D[j - 1] : 0) + cost(j, i);
	}

	class P {
		int i, j;

		public P(int i_, int j_) {
			i = i_;
			j = j_;
		}
	}

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