結果

問題 No.952 危険な火薬庫
ユーザー Shuz*Shuz*
提出日時 2019-12-01 12:20:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 174 ms / 2,000 ms
コード長 1,649 bytes
コンパイル時間 849 ms
コンパイル使用メモリ 79,084 KB
実行使用メモリ 73,916 KB
最終ジャッジ日時 2024-05-01 03:55:04
合計ジャッジ時間 3,179 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 126 ms
49,908 KB
testcase_04 AC 121 ms
49,112 KB
testcase_05 AC 98 ms
40,088 KB
testcase_06 AC 150 ms
59,148 KB
testcase_07 AC 49 ms
21,852 KB
testcase_08 AC 157 ms
66,900 KB
testcase_09 AC 174 ms
67,776 KB
testcase_10 AC 63 ms
28,304 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 69 ms
29,880 KB
testcase_13 AC 71 ms
31,196 KB
testcase_14 AC 16 ms
9,640 KB
testcase_15 AC 106 ms
44,788 KB
testcase_16 AC 20 ms
11,552 KB
testcase_17 AC 17 ms
9,456 KB
testcase_18 AC 26 ms
14,720 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 3 ms
6,940 KB
testcase_21 AC 61 ms
27,312 KB
testcase_22 AC 13 ms
8,260 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 7 ms
6,940 KB
testcase_25 AC 160 ms
73,916 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:30:14: warning: extra tokens at end of #undef directive
   30 |     #undef a first
      |              ^~~~~
main.cpp:31:14: warning: extra tokens at end of #undef directive
   31 |     #undef b second
      |              ^~~~~~
main.cpp:32:18: warning: extra tokens at end of #undef directive
   32 |     #undef lines deq.size()
      |                  ^~~

ソースコード

diff #

#include <cmath>
#include <deque>
#include <iostream>
using namespace std;
using ll = long long;
const ll INF = 1e18;

struct ConvexHullTrick {
    using F = pair<ll, ll>;
    deque<F> deq;
#define a first
#define b second
#define lines deq.size()
    bool invalid_f1(F f0, F f1, F f2) {
        return (__int128) (f1.a - f0.a) * (f2.b - f1.b) >= (__int128) (f1.b - f0.b) * (f2.a - f1.a);
    }
    ll f(F f0, ll x) { return f0.a * x + f0.b; }
    void add(F f0) {
        while (lines >= 2 && invalid_f1(deq[lines - 2], deq[lines - 1], f0)) {
            deq.pop_back();
        }
        deq.push_back(f0);
    }
    ll min(ll x) {
        while (lines >= 2 && f(deq[0], x) >= f(deq[1], x)) {
            deq.pop_front();
        }
        return f(deq[0], x);
    }
    #undef a first
    #undef b second
    #undef lines deq.size()
};

int main() {
    int N;
    cin >> N;
    ll S[N + 1], DP[N + 2][N + 1];
    S[0] = 0;
    for (int i = 0; i < N; i++) cin >> S[i + 1], S[i + 1] += S[i];
    for (int i = 0; i < N + 2; i++) {
        for (int j = 0; j < N + 1; j++) DP[i][j] = INF;
    }
    for (int i = 0; i < N + 2; i++) DP[i][0] = 0;
    for (int i = 0; i < N + 2; i++) {
        ConvexHullTrick CHT;
        for (int j = 0; j < N + 1; j++) {
            if (i + j + 1 > N + 1) break;
            ll A = -2 * S[i + j], B = S[i + j] * S[i + j] + DP[i + j][j];
            ll X = S[i + j], Y = S[i + j] * S[i + j];
            CHT.add(make_pair(A, B));
            DP[i + j + 1][j] = min(DP[i + j + 1][j], CHT.min(X) + Y);
        }
    }
    for (int i = 0; i < N; i++) {
        cout << min(DP[N][i + 1], DP[N + 1][i + 1]) << endl;
    }
}
0