結果

問題 No.952 危険な火薬庫
ユーザー koprickykopricky
提出日時 2019-12-15 12:32:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 1,267 bytes
コンパイル時間 1,861 ms
コンパイル使用メモリ 167,796 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-15 15:31:20
合計ジャッジ時間 3,235 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#define ull unsigned long long

using namespace std;

const int MAX_N = 3005;

ull sm[MAX_N], dp[2][MAX_N], ans[MAX_N];
int pos[2][MAX_N];

#define getchar getchar_unlocked
#define putchar putchar_unlocked

inline int in() {
    int n = 0; short c;
    while ((c = getchar()) >= '0') n = n * 10 + c - '0';
    return n;
}

inline void out(ull n) {
    short res[20], i = 0;
    do { res[i++] = n % 10, n /= 10; } while (n);
    while (i) putchar(res[--i] + '0');
    putchar('\n');
}

int main()
{
    int i, j, k, n = in();
    ull res;
    for(i = 0; i < n; ++i) sm[i+1] = sm[i] + in();
    for(i = 0; i < n+1; ++i) dp[0][i] = (1LL << 60);
    for(j = 1; j <= n; ++j){
        auto dp1 = dp[(j+1)&1]; auto dp2 = dp[j&1];
        auto pos1 = pos[(j+1)&1]; auto pos2 = pos[j&1];
        for(i = j; i <= n; ++i) dp2[i+1] = (1LL << 60);
        dp1[j-1] = 0, pos2[n+2] = n;
        for(i = n; i >= j; --i){
            for(k = pos1[i+1]; k <= pos2[i+2]; ++k){
                res = dp1[k] + (sm[i] - sm[k]) * (sm[i] - sm[k]);
                if(dp2[i+1] > res) dp2[i+1] = res, pos2[i+1] = k;
            }
        }
        ans[n-j] = dp2[n+1];
    }
    for(i = 0; i < n; ++i) out(ans[i]);
    return 0;
}
0