結果

問題 No.409 ダイエット
ユーザー btkbtk
提出日時 2016-04-18 17:09:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 2,688 bytes
コンパイル時間 1,504 ms
コンパイル使用メモリ 169,716 KB
実行使用メモリ 20,348 KB
最終ジャッジ日時 2023-09-08 16:02:18
合計ジャッジ時間 7,259 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 3 ms
4,376 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 2 ms
4,376 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 2 ms
4,380 KB
testcase_49 AC 2 ms
4,376 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 2 ms
4,376 KB
testcase_52 AC 2 ms
4,376 KB
testcase_53 AC 2 ms
4,376 KB
testcase_54 AC 2 ms
4,380 KB
testcase_55 AC 20 ms
11,408 KB
testcase_56 AC 30 ms
11,820 KB
testcase_57 AC 36 ms
20,348 KB
testcase_58 AC 18 ms
10,968 KB
testcase_59 AC 24 ms
12,664 KB
testcase_60 AC 17 ms
10,576 KB
testcase_61 AC 32 ms
19,336 KB
testcase_62 AC 33 ms
19,864 KB
testcase_63 AC 32 ms
17,108 KB
testcase_64 AC 19 ms
11,580 KB
testcase_65 AC 34 ms
15,312 KB
testcase_66 AC 36 ms
20,128 KB
testcase_67 AC 27 ms
16,072 KB
testcase_68 AC 24 ms
12,748 KB
testcase_69 AC 31 ms
17,164 KB
testcase_70 AC 36 ms
16,552 KB
testcase_71 AC 21 ms
11,108 KB
testcase_72 AC 44 ms
20,072 KB
testcase_73 AC 42 ms
17,424 KB
testcase_74 AC 27 ms
14,264 KB
testcase_75 AC 42 ms
17,364 KB
testcase_76 AC 30 ms
15,056 KB
testcase_77 AC 22 ms
9,000 KB
testcase_78 AC 23 ms
13,720 KB
testcase_79 AC 20 ms
10,568 KB
testcase_80 AC 42 ms
19,552 KB
testcase_81 AC 41 ms
17,320 KB
testcase_82 AC 31 ms
15,380 KB
testcase_83 AC 34 ms
15,856 KB
testcase_84 AC 29 ms
12,428 KB
testcase_85 AC 5 ms
4,380 KB
testcase_86 AC 28 ms
12,296 KB
testcase_87 AC 37 ms
18,624 KB
testcase_88 AC 17 ms
8,008 KB
testcase_89 AC 33 ms
10,088 KB
testcase_90 AC 16 ms
6,408 KB
testcase_91 AC 34 ms
10,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 INT;


/*cin高速化*/
struct INIT{INIT(){ios::sync_with_stdio(false);cin.tie(0);}}init;

ostream& operator<<(ostream &os,INT x){
    LL y;
    y=x;
    assert(y==x);
    os << y;
    return os;
}
istream& operator>>(istream &is,INT& x){
    LL y;
    is >> y;
    x = y;
    return is;
}

const INT INF = 1e24;


namespace CHT{
using TYPE=INT;
#define A first
#define B second
#define SIZE 1000000
    pair<TYPE,TYPE> mem[SIZE];
    template<typename T>
    inline bool is_minimam(T a1,T b1,T a2,T b2,T a3,T b3){return (a2-a1)*(b3-b2)>=(b2-b1)*(a3-a2);}
    template<typename T>
    inline bool is_maximam(T a1,T b1,T a2,T b2,T a3,T b3){return is_minimam(-a1,-b1,-a2,-b2,-a3,-b3);}
    template<typename T>
    inline bool check(pair<T,T> L1,pair<T,T> L2,pair<T,T> L3){
        return is_minimam(L1.A,L1.B,L2.A,L2.B,L3.A,L3.B);
    }
    template<typename T>
    inline T calc(pair<T,T> L,T now){
        return L.A*now+L.B;
    }
};
template<typename T>
ostream& operator<<(ostream& os,pair<T,T> t){
    os <<"("<< t.first << " "<<t.second << ")";
    return os;
}
#define mp(a,b) std::make_pair(a,b)
template<typename T>
class convex_hull_trick{
    pair<T,T> *deque;
    int s,t;
public:
    convex_hull_trick(pair<T,T> *mem):deque(mem),s(0),t(0){}
    convex_hull_trick():convex_hull_trick(CHT::mem){}
    void add(T a,T b){
        auto L=mp(a,b);
        while(s+1<t&&CHT::check(deque[t-2],deque[t-1],L))t--;
        deque[t++]=L;
    }
    T get(T now){
        while(s+1<t&&CHT::calc(deque[s],now)>=CHT::calc(deque[s+1],now))s++;
        return CHT::calc(deque[s],now);
    }
    void pop(){
        while(s+1<t&&CHT::calc(deque[s])>=CHT::calc(deque[s+1]))s++;
    }
};


int main() {
    INT N,A,B,W;
    /*input N,A,B,W*/
    {
        cin >> N >> A >> B >> W;
        assert(1 <= N && N <= 300000);
        assert(0 <= A && A <= 1000000);
        assert(0 <= B && B <= 1000000);
        assert(0 <= W && W <= 1000000);
    }
    vector<INT> D(N);
    {
        for(auto& it : D){
            cin >> it;
            assert(0 <= it && it <= 1000000);
        }
    }


    /*DP*/
    convex_hull_trick<INT> deq;
    vector<INT> dp(N+1,INF);
    dp[0] = W;
    deq.add( - 0 * B,dp[0] + 0 * A + B * 0 * (0 - 1) / 2);
    for(int i = 0; i < N; i++){
        dp[i + 1] = D[i] - i * A + ( B * i * (i + 1)) / 2 + deq.get(i);
        deq.add( - (i + 1) * B , dp[i + 1] + (i + 1) * A + ( B * (i + 1) * i) / 2);
    }

    /*output*/
    INT res = INF;
    for(int i = 0; i <= N; i++)
        res = min(res,dp[i] - A * (N -i) + B * (N - i) * (N - i + 1) / 2);
    cout << res << endl;
    return 0;
}
0