結果

問題 No.952 危険な火薬庫
ユーザー ianCKianCK
提出日時 2019-12-16 23:19:07
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 351 ms / 2,000 ms
コード長 1,559 bytes
コンパイル時間 303 ms
コンパイル使用メモリ 42,048 KB
実行使用メモリ 73,628 KB
最終ジャッジ日時 2023-09-15 18:50:54
合計ジャッジ時間 4,366 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 249 ms
60,388 KB
testcase_04 AC 249 ms
60,368 KB
testcase_05 AC 196 ms
53,988 KB
testcase_06 AC 305 ms
66,488 KB
testcase_07 AC 91 ms
39,220 KB
testcase_08 AC 342 ms
70,572 KB
testcase_09 AC 351 ms
70,664 KB
testcase_10 AC 128 ms
45,564 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 141 ms
47,672 KB
testcase_13 AC 150 ms
47,624 KB
testcase_14 AC 30 ms
24,460 KB
testcase_15 AC 226 ms
58,192 KB
testcase_16 AC 39 ms
26,584 KB
testcase_17 AC 30 ms
24,432 KB
testcase_18 AC 54 ms
30,804 KB
testcase_19 AC 2 ms
5,424 KB
testcase_20 AC 2 ms
5,484 KB
testcase_21 AC 122 ms
45,468 KB
testcase_22 AC 24 ms
20,304 KB
testcase_23 AC 3 ms
5,552 KB
testcase_24 AC 11 ms
13,972 KB
testcase_25 AC 321 ms
73,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

bool debug = false;
#include <stdio.h>
#include <deque>
using namespace std;
typedef long long int ll;
constexpr int kN = int(3E3 + 10);
constexpr ll kInf = ll(1E16 + 10);
struct From {
	ll A, B, lim, st;
	int id;
	From(ll a, ll b, ll c, ll d, int e){A = a, B = b, lim = c, st = d, id = e;}
	From(){}
	ll operator ()(ll x){return A * x + B;}
};
int n;
ll a[kN], s[kN], ans[kN], dp[kN][kN];
int main() {
	ll top, nxt;
	From tmp;
	deque<From> dq;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
	s[0] = 0;
	for (int i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];
	for (int i = 1; i <= n; i++) dp[0][i] = 0;
	for (int i = 1; i <= n; i++) dp[i][0] = s[i] * s[i];
	for (int i = 1; i <= n; i++) {
		top = 0;
		dq.clear();
		dp[1][i] = 0;
		dq.push_back(From(-(s[1] << 1), s[1] * s[1], kInf, 0, 0));
		for (int j = 1; j <= n; j++) {
			while (!dq.empty()) {
				if (dq[0].lim < s[j]) dq.pop_front();
				else break;
			}
			dp[j][i] = dq.front()(s[j]) + s[j] * s[j];
			if (debug) printf("dp[%d][%d] = %lld, dq.front() = %lld, %lld, id = %d\n", j, i, dp[j][i], dq.front().A, dq.front().B, dq.front().id);
			tmp = From(-(s[j + 1] << 1), dp[j][i - 1] + s[j + 1] * s[j + 1], kInf, 0, j); 
			while (!dq.empty()) {
				nxt = (tmp.B - dq.back().B - 1) / (dq.back().A - tmp.A);
				if (dq.back().st > nxt) dq.pop_back();
				else {
					dq.back().lim = nxt;
					tmp.st = nxt + 1;
					break;
				}
			}
			dq.push_back(tmp);
		}
	}

	for (int i = 1; i <= n; i++) printf("%lld\n", dp[n][n - i]);
}
0