結果

問題 No.409 ダイエット
ユーザー koprickykopricky
提出日時 2018-04-25 04:57:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 3,829 bytes
コンパイル時間 1,354 ms
コンパイル使用メモリ 152,804 KB
実行使用メモリ 17,740 KB
最終ジャッジ日時 2023-09-10 05:21:02
合計ジャッジ時間 5,832 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 2 ms
4,376 KB
testcase_47 AC 2 ms
4,376 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,376 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 2 ms
4,376 KB
testcase_52 AC 2 ms
4,376 KB
testcase_53 AC 2 ms
4,380 KB
testcase_54 AC 2 ms
4,376 KB
testcase_55 AC 19 ms
10,216 KB
testcase_56 AC 35 ms
17,364 KB
testcase_57 AC 37 ms
17,740 KB
testcase_58 AC 16 ms
9,428 KB
testcase_59 AC 24 ms
11,940 KB
testcase_60 AC 15 ms
8,512 KB
testcase_61 AC 33 ms
15,900 KB
testcase_62 AC 35 ms
16,988 KB
testcase_63 AC 32 ms
15,636 KB
testcase_64 AC 18 ms
9,844 KB
testcase_65 AC 35 ms
16,652 KB
testcase_66 AC 37 ms
17,608 KB
testcase_67 AC 27 ms
13,864 KB
testcase_68 AC 23 ms
11,676 KB
testcase_69 AC 32 ms
15,368 KB
testcase_70 AC 37 ms
15,020 KB
testcase_71 AC 21 ms
9,620 KB
testcase_72 AC 44 ms
17,424 KB
testcase_73 AC 42 ms
16,284 KB
testcase_74 AC 26 ms
11,536 KB
testcase_75 AC 42 ms
16,556 KB
testcase_76 AC 30 ms
12,468 KB
testcase_77 AC 21 ms
9,420 KB
testcase_78 AC 23 ms
10,360 KB
testcase_79 AC 19 ms
8,892 KB
testcase_80 AC 43 ms
16,836 KB
testcase_81 AC 42 ms
16,288 KB
testcase_82 AC 31 ms
12,856 KB
testcase_83 AC 33 ms
13,520 KB
testcase_84 AC 28 ms
11,800 KB
testcase_85 AC 5 ms
4,380 KB
testcase_86 AC 28 ms
11,476 KB
testcase_87 AC 37 ms
14,948 KB
testcase_88 AC 16 ms
7,868 KB
testcase_89 AC 33 ms
13,000 KB
testcase_90 AC 16 ms
7,460 KB
testcase_91 AC 34 ms
13,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define ll long long
#define INF (1LL << 60)
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

ll A,B,W;
vl d;

//f[j](x)=a[j]x+b[j]でaがjの増加に伴い単調減少する場合
//0<=j<=i-kを満たす直線f[j](x)についてminをとる
//maxをとる場合はcheckの不等号とsolve内のsの移動の部分の不等号を逆にする
template<typename T> class CHT
{
public:
    //deqには直線のインデックスを格納
    //dpの初期値はINFづめしている
    vector<int> deq;
    vector<T> a,b,dp;
    int n,k;
    CHT(vector<T>& ag1,vector<T>& ag2,int sz){
        a = ag1, b = ag2;
        n = (int)a.size(), k = sz;
        deq.resize(n),dp.resize(n+1,INF);
        solve();
    }
    T func(int j,int x){
        return a[j]*x+b[j];
    }
    bool check(int j1, int j2, int j3){
        return (a[j2]-a[j1])*(b[j3]-b[j2]) >= (b[j2]-b[j1])*(a[j3]-a[j2]);
    }
    //今回の場合は0<=j<=i-kを満たす直線f[j](x)についてminをとっている
    void solve(){
        dp[0] = 0, deq[0] = 0;
        int s = 0, t = 1;
        //今回の場合はb[i]の値がdp[i]の値に依存するのでb[i]を更新する
        rep(i,k){
            b[i] += dp[i];
        }
        //今回の問題の場合i=k~2k-1まではj=0の場合が最適であることが明らかなので
        //i-k<kの間はdeqに新たに直線を追加することはしない
        for(ll i=k;i<=n;i++){
            if(i-k >= k){
                //末端から最小値を取る可能性がなくなったものを取り除く
                while(s+1 < t && check(deq[t-2],deq[t-1],i-k)){
                    t--;
                }
                deq[t++] = i-k;
            }
            //先頭が最小値でなければ取り除く
            while(s+1 < t && func(deq[s],i) >= func(deq[s+1],i)){
                s++;
            }
            dp[i] = func(deq[s],i) - (i-1)*A + (i*i-i)*B/2 + d[i-1];
            //今回の場合はb[i]の値がdp[i]の値に依存するのでb[i]を更新する
            if(i < n){
                b[i] += dp[i];
            }
        }
    }
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n;
    cin >> n >> A >> B >> W;
    d.resize(n);
    rep(i,n){
        cin >> d[i];
    }
    vector<ll> a(n), b(n);
    rep(i,n){
        a[i] = -i*B;
    }
    rep(i,n){
        b[i] = i*A+((ll)i*i+i)*B/2;
    }
    CHT<ll> cht(a,b,1);
    ll ans = INF;
    rep(i,n+1){
        cmn(ans,cht.dp[i]-(n-i)*A+(n-i)*(n-i+1)*B/2);
    }
    cout << W + ans << "\n";
    return 0;
}
0