結果

問題 No.952 危険な火薬庫
ユーザー square1001square1001
提出日時 2020-01-31 20:34:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 165 ms / 2,000 ms
コード長 1,348 bytes
コンパイル時間 874 ms
コンパイル使用メモリ 81,224 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 08:29:43
合計ジャッジ時間 3,559 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 119 ms
4,348 KB
testcase_04 AC 117 ms
4,348 KB
testcase_05 AC 93 ms
4,348 KB
testcase_06 AC 141 ms
4,348 KB
testcase_07 AC 48 ms
4,348 KB
testcase_08 AC 162 ms
4,348 KB
testcase_09 AC 164 ms
4,348 KB
testcase_10 AC 64 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 68 ms
4,348 KB
testcase_13 AC 71 ms
4,348 KB
testcase_14 AC 17 ms
4,348 KB
testcase_15 AC 104 ms
4,348 KB
testcase_16 AC 22 ms
4,348 KB
testcase_17 AC 17 ms
4,348 KB
testcase_18 AC 30 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 60 ms
4,348 KB
testcase_22 AC 14 ms
4,348 KB
testcase_23 AC 3 ms
4,348 KB
testcase_24 AC 7 ms
4,348 KB
testcase_25 AC 165 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <deque>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const long long inf = 1LL << 62;
class linear {
public:
	long long a, b;
	linear() : a(0), b(0) {};
	linear(long long a_, long long b_) : a(a_), b(b_) {};
	long long get(long long x) const {
		return a * x + b;
	}
	double collide(const linear& l) const {
		return -double(a - l.a) / (b - l.b);
	}
};
int main() {
	int N;
	cin >> N;
	vector<long long> A(N);
	for (int i = 0; i < N; ++i) {
		cin >> A[i];
	}
	vector<long long> S(N + 1);
	for (int i = 0; i < N; ++i) {
		S[i + 1] = S[i] + A[i];
	}
	vector<long long> dp(N + 1, inf);
	dp[0] = 0;
	vector<long long> ans(N + 1, inf);
	for (int i = N; i >= 1; --i) {
		for (int j = 0; j <= N; ++j) {
			ans[i] = min(ans[i], dp[j] + S[N] * (S[N] - 2 * S[j]));
		}
		deque<linear> que;
		vector<long long> ndp(N + 1, inf);
		for (int j = 1; j <= N; ++j) {
			linear ln(-2 * S[j - 1], dp[j - 1]);
			while (que.size() >= 2 && que[que.size() - 2].collide(ln) < que[que.size() - 1].collide(ln)) {
				que.pop_back();
			}
			que.push_back(ln);
			while (que.size() >= 2 && que[0].get(S[j - 1]) > que[1].get(S[j - 1])) {
				que.pop_front();
			}
			ndp[j] = que.front().get(S[j - 1]) + S[j - 1] * S[j - 1] + S[j] * S[j];
		}
		dp = ndp;
	}
	for (int i = 1; i <= N; ++i) {
		cout << ans[i] << '\n';
	}
	return 0;
}
0