結果
問題 | No.952 危険な火薬庫 |
ユーザー |
![]() |
提出日時 | 2020-01-31 20:34:59 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 166 ms / 2,000 ms |
コード長 | 1,348 bytes |
コンパイル時間 | 886 ms |
コンパイル使用メモリ | 81,356 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-17 06:59:01 |
合計ジャッジ時間 | 3,105 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 23 |
ソースコード
#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;}