結果

問題 No.952 危険な火薬庫
ユーザー 37zigen37zigen
提出日時 2019-12-16 08:19:01
言語 Java19
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,181 bytes
コンパイル時間 4,286 ms
コンパイル使用メモリ 74,200 KB
実行使用メモリ 285,116 KB
最終ジャッジ日時 2023-09-15 17:10:56
合計ジャッジ時間 17,378 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
56,084 KB
testcase_01 WA -
testcase_02 AC 116 ms
55,760 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 772 ms
285,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.Arrays;
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[] A = new long[N + 1];
		long[] S = new long[N + 1];
		long[] a = new long[N + 1];
		long[][] b = new long[N + 1][N + 1];
		for (int i = 0; i < N; ++i) {
			A[i] = sc.nextLong();
		}
		++N;
		for (int i = 0; i < N; ++i) {
			S[i] = A[i] + (i > 0 ? S[i - 1] : 0);
			a[i] = -2 * S[i];
		}
		for (int i = 0; i < N; ++i) {
			b[i][0] = S[i] * S[i] + (i > 0 ? S[i - 1] * S[i - 1] : 0);
		}

		long[][] dp = new long[N + 1][N + 1];
		for (int i = 0; i < dp.length; ++i) {
			for (int j = 0; j < dp[i].length; ++j) {
				if (j == 0) {
					dp[i][j] = (i > 0 ? S[i - 1] * S[i - 1] : 0);
				} else {
					dp[i][j] = Long.MAX_VALUE / 3;
				}
			}
		}
		for (int D = 0; D < N; ++D) {
			long[] dq_a = new long[N];
			long[] dq_b = new long[N];
			Arrays.fill(dq_a, Long.MAX_VALUE / 3);
			Arrays.fill(dq_b, Long.MAX_VALUE / 3);
			int s = 0, t = 0;
			for (int i = 0; i < N; ++i) {
				int j = (i + 1) - (D + 1);
				if (j < 0 || j > i + 1)
					continue;
				long dq_a0 = a[i];
				long dq_b0 = b[i][D];
				// a1x+b1=a2x+b2
				// a0-a1<0
				// invalid な 条件
				// x=-(b0-b1)/(a0-a1)<-(b2-b1)/(a2-a1)
				// (b0-b1)(a2-a1)>(b2-b1)(a0-a1)
				while (t - s >= 2 && (dq_b0 - dq_b[t - 1]) * (dq_a[t - 2] - dq_a[t - 1]) <= (dq_b[t - 2] - dq_b[t - 1])
						* (dq_a0 - dq_a[t - 1])) {
					--t;
				}
				dq_a[t] = dq_a0;
				dq_b[t] = dq_b0;
				++t;
				if (t - s >= 2 && dq_a[s] * S[i] + dq_b[s] >= dq_a[s + 1] * S[i] + dq_b[s + 1]) {
					++s;
				}
				if (i + 1 < N) {
					dp[i + 1][D + 1] = S[i] * S[i] + dq_a[s] * S[i] + dq_b[s];
					if (D + 1 < N) {
						b[i + 1][D + 1] = S[i + 1] * S[i + 1] + dp[i + 1][D + 1];
					}
				}
			}
		}
		long[] ans = new long[N + 1];
		Arrays.fill(ans, Long.MAX_VALUE / 3);
		for (int j = 1; j < N; ++j) {
			int D = (N - 1) - j;
			ans[j] = dp[N - 1][D];// dp[N-1][N-1]
			System.out.println(ans[j]);
		}
	}

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