結果

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

テストケース

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

ソースコード

diff #

bool debug = false;
#include <stdio.h>
#include <deque>
using namespace std;
typedef long long int ll;
constexpr int kN = int(3E2 + 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