結果

問題 No.952 危険な火薬庫
ユーザー tarattata1tarattata1
提出日時 2019-12-15 22:10:09
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,699 bytes
コンパイル時間 1,811 ms
コンパイル使用メモリ 82,072 KB
実行使用メモリ 72,048 KB
最終ジャッジ日時 2023-09-15 15:45:43
合計ジャッジ時間 3,409 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996) 
 
typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
//const long long MOD = 998244353;
using namespace std;

ll dp[3005][3005];     // dp[i][j] i番目(0,1,..i-1)まで見て、j個閉じている場合の危険度の最小値

int main(int argc, char* argv[])
{
    int n;
    scanf("%d%", &n);
    vector<ll> A(n),S(n+1);
    int i,j;
    for(i=0; i<n; i++) {
        scanf("%lld", &A[i]);
        S[i+1]=S[i]+A[i];
    }

    vector<ll> a(n+1),b(n+1);

    const double eps=1e-6;
    for(j=0; j<=n; j++) {
        if(j==0) {
            for(i=0; i<=n; i++) {
                dp[i][j]=S[i]*S[i];
            }
        }
        else {
            vector<pair<double,int> > z;    // xの値, xより少し大きいところの直線のindex
            for(i=j; i<=n; i++) {
                if(i>j) {
                    ll tmp=0;
                    if(!z.empty()) {
                        int k=lower_bound(z.begin(), z.end(), make_pair((double)S[i]+eps,-INF))-z.begin();
                        if(k>0) {
                            k--;
                            int id=z[k].second;
                            tmp=a[id]*S[i]+b[id];
                        }
                    }
                    dp[i][j]=tmp+S[i]*S[i];
                }

                if(i<n) {
                    // for CHT
                    a[i]=-2*S[i+1];
                    b[i]=dp[i][j-1]+S[i+1]*S[i+1];

                    while( 1 ) {
                        if(z.empty()) {
                            z.push_back(make_pair(-LINF,i));
                            break;
                        }
                        else {
                            double  x0=z.back().first;
                            int id0=z.back().second;

                            double tmp=-(double)(b[i]-b[id0])/(a[i]-a[id0]);
                            if(tmp<x0) {
                                z.pop_back();
                            }
                            else {
                                z.push_back(make_pair(tmp,i));
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    for(i=0; i<n; i++) {
        printf("%lld\n", dp[n][n-1-i]);
    }

    return 0;
}
0