結果

問題 No.409 ダイエット
ユーザー face4face4
提出日時 2019-12-22 14:26:21
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 159 ms / 2,000 ms
コード長 2,163 bytes
コンパイル時間 953 ms
コンパイル使用メモリ 76,964 KB
実行使用メモリ 32,956 KB
最終ジャッジ日時 2023-10-12 07:05:31
合計ジャッジ時間 10,005 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,368 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 2 ms
4,368 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 1 ms
4,372 KB
testcase_06 AC 2 ms
4,368 KB
testcase_07 AC 2 ms
4,372 KB
testcase_08 AC 1 ms
4,368 KB
testcase_09 AC 2 ms
4,372 KB
testcase_10 AC 2 ms
4,372 KB
testcase_11 AC 1 ms
4,368 KB
testcase_12 AC 2 ms
4,368 KB
testcase_13 AC 2 ms
4,368 KB
testcase_14 AC 2 ms
4,368 KB
testcase_15 AC 2 ms
4,372 KB
testcase_16 AC 1 ms
4,368 KB
testcase_17 AC 2 ms
4,372 KB
testcase_18 AC 1 ms
4,372 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,372 KB
testcase_21 AC 2 ms
4,368 KB
testcase_22 AC 2 ms
4,372 KB
testcase_23 AC 1 ms
4,368 KB
testcase_24 AC 1 ms
4,368 KB
testcase_25 AC 2 ms
4,372 KB
testcase_26 AC 2 ms
4,372 KB
testcase_27 AC 1 ms
4,368 KB
testcase_28 AC 1 ms
4,372 KB
testcase_29 AC 1 ms
4,368 KB
testcase_30 AC 1 ms
4,372 KB
testcase_31 AC 1 ms
4,368 KB
testcase_32 AC 2 ms
4,368 KB
testcase_33 AC 2 ms
4,368 KB
testcase_34 AC 2 ms
4,372 KB
testcase_35 AC 2 ms
4,368 KB
testcase_36 AC 3 ms
4,368 KB
testcase_37 AC 3 ms
4,368 KB
testcase_38 AC 3 ms
4,368 KB
testcase_39 AC 3 ms
4,372 KB
testcase_40 AC 3 ms
4,368 KB
testcase_41 AC 3 ms
4,372 KB
testcase_42 AC 3 ms
4,368 KB
testcase_43 AC 3 ms
4,368 KB
testcase_44 AC 3 ms
4,368 KB
testcase_45 AC 3 ms
4,372 KB
testcase_46 AC 3 ms
4,368 KB
testcase_47 AC 3 ms
4,368 KB
testcase_48 AC 3 ms
4,372 KB
testcase_49 AC 3 ms
4,368 KB
testcase_50 AC 3 ms
4,372 KB
testcase_51 AC 3 ms
4,368 KB
testcase_52 AC 3 ms
4,372 KB
testcase_53 AC 3 ms
4,372 KB
testcase_54 AC 3 ms
4,368 KB
testcase_55 AC 63 ms
16,700 KB
testcase_56 AC 77 ms
30,280 KB
testcase_57 AC 127 ms
32,056 KB
testcase_58 AC 52 ms
11,432 KB
testcase_59 AC 76 ms
17,688 KB
testcase_60 AC 47 ms
10,880 KB
testcase_61 AC 107 ms
20,184 KB
testcase_62 AC 121 ms
30,516 KB
testcase_63 AC 107 ms
20,796 KB
testcase_64 AC 59 ms
16,612 KB
testcase_65 AC 116 ms
30,064 KB
testcase_66 AC 124 ms
30,136 KB
testcase_67 AC 92 ms
18,464 KB
testcase_68 AC 75 ms
17,476 KB
testcase_69 AC 104 ms
19,980 KB
testcase_70 AC 127 ms
18,964 KB
testcase_71 AC 71 ms
11,252 KB
testcase_72 AC 159 ms
30,980 KB
testcase_73 AC 139 ms
19,880 KB
testcase_74 AC 91 ms
17,296 KB
testcase_75 AC 150 ms
32,956 KB
testcase_76 AC 104 ms
17,756 KB
testcase_77 AC 69 ms
11,428 KB
testcase_78 AC 81 ms
17,000 KB
testcase_79 AC 63 ms
11,176 KB
testcase_80 AC 151 ms
29,928 KB
testcase_81 AC 148 ms
20,392 KB
testcase_82 AC 106 ms
18,284 KB
testcase_83 AC 113 ms
18,592 KB
testcase_84 AC 93 ms
17,440 KB
testcase_85 AC 12 ms
5,064 KB
testcase_86 AC 93 ms
17,272 KB
testcase_87 AC 129 ms
18,836 KB
testcase_88 AC 52 ms
10,392 KB
testcase_89 AC 102 ms
18,688 KB
testcase_90 AC 48 ms
10,456 KB
testcase_91 AC 111 ms
18,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;

struct line{
    ll a, b;
    bool operator==(const line other)const{
        return a==other.a && b==other.b;
    }
};

ll f(line l, ll x){
    return l.a*x + l.b;
}

const ll INF = 1ll<<55;

struct LiChaoTree{
    int n;
    vector<ll> xs;
    vector<line> v;
    line tmp{0, INF};
    LiChaoTree(vector<ll> x) : xs(x){
        n = 1;
        while(n < xs.size())  n *= 2;
        while(xs.size() < n)    xs.push_back(x.back());
        v.resize(2*n+1, tmp);
    }
    void add_line(line p, int k=0, int l=0, int r=-1){
        if(r < 0)   r = n;
        int m = (l+r)/2;
        if(v[k] == tmp){
            v[k] = p;
            return;
        }
        ll lx = xs[l], mx = xs[m], rx = xs[r-1];
        bool left = f(p,lx) < f(v[k], lx);
        bool mid = f(p,mx) < f(v[k], mx);
        bool right = f(p,rx) < f(v[k], rx);
        if(left == right){
            if(left)    v[k] = p;
            return;
        }
        if(mid){
            swap(v[k], p);
        }
        // v[k]とpが交わっている部分について更新していくと考えるとわかりやすい
        if(left != mid){
            add_line(p, 2*k+1, l, m);
        }else{
            add_line(p, 2*k+2, m, r);
        }
    }
    ll query(int k){
        ll x = xs[k];
        k += n-1;
        ll ret = INF;
        while(true){
            if(!(v[k]==tmp)) ret = min(ret, f(v[k], x));
            if(k==0)    break;
            k = (k-1)/2;
        }
        return ret;
    }
};

int main(){
    ll n, a, b, w;
    cin >> n >> a >> b >> w;
    vector<ll> d(n);
    for(int i = 0; i < n; i++)  cin >> d[i];
    vector<ll> v;
    for(int i = 0; i <= n; i++) v.push_back(i);
    LiChaoTree lct(v);
    vector<ll> dp(n+1, INF);
    dp[0] = 0;
    lct.add_line(line({0,0}));
    for(ll i = 1; i <= n; i++){
        dp[i] = lct.query(i) - a*(i-1) + (i-1)*i/2*b + d[i-1];
        lct.add_line(line({-i*b, dp[i]+a*i+i*(i+1)/2*b}));
    }
    ll ans = 1ll<<60;
    for(int i = 0; i <= n; i++){
        ans = min(ans, w+dp[i]-a*(n-i)+(n-i)*(n-i+1)/2*b);
    }
    cout << ans << endl;
    return 0;
}
0