結果

問題 No.952 危険な火薬庫
ユーザー 37zigen37zigen
提出日時 2019-12-16 21:59:30
言語 Java21
(openjdk 21)
結果
AC  
実行時間 928 ms / 2,000 ms
コード長 2,307 bytes
コンパイル時間 2,393 ms
コンパイル使用メモリ 74,860 KB
実行使用メモリ 285,636 KB
最終ジャッジ日時 2023-09-15 18:33:16
合計ジャッジ時間 15,309 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
56,156 KB
testcase_01 AC 124 ms
55,884 KB
testcase_02 AC 125 ms
55,680 KB
testcase_03 AC 741 ms
191,548 KB
testcase_04 AC 726 ms
191,856 KB
testcase_05 AC 646 ms
175,608 KB
testcase_06 AC 798 ms
242,300 KB
testcase_07 AC 469 ms
113,432 KB
testcase_08 AC 928 ms
265,380 KB
testcase_09 AC 901 ms
272,372 KB
testcase_10 AC 533 ms
142,588 KB
testcase_11 AC 125 ms
55,824 KB
testcase_12 AC 510 ms
148,088 KB
testcase_13 AC 564 ms
150,492 KB
testcase_14 AC 331 ms
77,280 KB
testcase_15 AC 683 ms
183,972 KB
testcase_16 AC 357 ms
79,560 KB
testcase_17 AC 336 ms
77,196 KB
testcase_18 AC 400 ms
95,052 KB
testcase_19 AC 136 ms
56,232 KB
testcase_20 AC 149 ms
56,768 KB
testcase_21 AC 541 ms
138,148 KB
testcase_22 AC 303 ms
72,212 KB
testcase_23 AC 151 ms
57,900 KB
testcase_24 AC 245 ms
62,688 KB
testcase_25 AC 872 ms
285,636 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();
		int add=1;
		long[] A = new long[N + add];
		long[] S = new long[N + add];
		long[] a = new long[N + add];
		long[][] b = new long[N + add][N + add];
		for (int i = 0; i < N; ++i) {
			A[i] = sc.nextLong();
		}
		for(int i=0;i<add;++i){
			A[N++]=(long)1e6;
		}
		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<b.length;++i){
			for(int j=0;j<b[i].length;++j){
				b[i][j]=Long.MAX_VALUE;
			}
		}
		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][N];
		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)
					continue;
				long dq_a0 = a[i];
				long dq_b0 = b[i][D];
				// -(b0-b1)/(a0-a1)<-(b2-b1)/(a2-a1)
				// (b0-b1)(a2-a1)<(b2-b1)(a0-a1)
				while (t - s >= 2 && (double)(dq_b0 - dq_b[t - 1]) * (dq_a[t - 2] - dq_a[t - 1]) <= (double)(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;
				while (t - s >= 2 && (double)dq_a[s] * S[i] + dq_b[s] >= (double)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-add; ++j) {
			int D = N - 1 - j;
			ans[j] = Math.min(ans[j], dp[N - 1][D]);
			System.out.println(ans[j]);
		}
	}

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