結果

問題 No.409 ダイエット
ユーザー raven7959raven7959
提出日時 2022-05-24 13:56:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 98 ms / 2,000 ms
コード長 5,724 bytes
コンパイル時間 1,915 ms
コンパイル使用メモリ 207,256 KB
実行使用メモリ 21,376 KB
最終ジャッジ日時 2024-09-20 14:22:48
合計ジャッジ時間 7,802 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 3 ms
5,376 KB
testcase_36 AC 3 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 3 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 3 ms
5,376 KB
testcase_44 AC 3 ms
5,376 KB
testcase_45 AC 2 ms
5,376 KB
testcase_46 AC 2 ms
5,376 KB
testcase_47 AC 3 ms
5,376 KB
testcase_48 AC 3 ms
5,376 KB
testcase_49 AC 2 ms
5,376 KB
testcase_50 AC 3 ms
5,376 KB
testcase_51 AC 2 ms
5,376 KB
testcase_52 AC 2 ms
5,376 KB
testcase_53 AC 3 ms
5,376 KB
testcase_54 AC 2 ms
5,376 KB
testcase_55 AC 45 ms
11,776 KB
testcase_56 AC 22 ms
7,680 KB
testcase_57 AC 91 ms
21,376 KB
testcase_58 AC 41 ms
10,752 KB
testcase_59 AC 54 ms
12,416 KB
testcase_60 AC 35 ms
10,112 KB
testcase_61 AC 82 ms
18,688 KB
testcase_62 AC 88 ms
20,224 KB
testcase_63 AC 81 ms
18,432 KB
testcase_64 AC 43 ms
11,136 KB
testcase_65 AC 77 ms
15,488 KB
testcase_66 AC 88 ms
20,096 KB
testcase_67 AC 68 ms
16,640 KB
testcase_68 AC 54 ms
12,672 KB
testcase_69 AC 78 ms
17,792 KB
testcase_70 AC 80 ms
17,792 KB
testcase_71 AC 44 ms
11,392 KB
testcase_72 AC 98 ms
21,120 KB
testcase_73 AC 86 ms
17,408 KB
testcase_74 AC 56 ms
13,440 KB
testcase_75 AC 91 ms
18,176 KB
testcase_76 AC 65 ms
14,976 KB
testcase_77 AC 42 ms
9,600 KB
testcase_78 AC 50 ms
12,288 KB
testcase_79 AC 41 ms
10,240 KB
testcase_80 AC 92 ms
19,968 KB
testcase_81 AC 90 ms
17,664 KB
testcase_82 AC 68 ms
15,232 KB
testcase_83 AC 72 ms
16,000 KB
testcase_84 AC 60 ms
11,648 KB
testcase_85 AC 8 ms
5,376 KB
testcase_86 AC 54 ms
12,160 KB
testcase_87 AC 81 ms
17,920 KB
testcase_88 AC 33 ms
8,448 KB
testcase_89 AC 54 ms
8,576 KB
testcase_90 AC 27 ms
6,400 KB
testcase_91 AC 58 ms
8,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,fma,abm,mmx,avx,avx2")
#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 all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define yn(joken) cout<<((joken) ? "Yes" : "No")<<"\n"
#define YN(joken) cout<<((joken) ? "YES" : "NO")<<"\n"
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vd = vector<double>;
using vld = vector<long double>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvs = vector<vector<string>>;
using vvc = vector<vector<char>>;
using vvd = vector<vector<double>>;
using vvld = vector<vector<long double>>;
using vvvi = vector<vector<vector<int>>>;
using vvvl = vector<vector<vector<ll>>>;
using vvvvi = vector<vector<vector<vector<int>>>>;
using vvvvl = vector<vector<vector<vector<ll>>>>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
const int INF = 1e9;
const ll LINF = 2e18;
template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}
bool ispow2(int i) { return i && (i & -i) == i; }
bool ispow2(ll i) { return i && (i & -i) == i; }
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
    for (int i = 0; i < int(v.size()); i++) {
        is >> v[i];
    }
    return is;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < int(v.size()); i++) {
        os << v[i];
        if (i < int(v.size()) - 1) os << ' ';
    }
    return os;
}

static uint32_t RandXor(){
    static uint32_t x=123456789;
    static uint32_t y=362436069;
    static uint32_t z=521288629;
    static uint32_t w=88675123;
    uint32_t t;
 
    t=x^(x<<11);
    x=y; y=z; z=w;
    return w=(w^(w>>19))^(t^(t>>8));
}

// DynamicLiChaoTree<ll> LCT(ll min_index,ll max_index); などする
// xの動きうる最大範囲が[min_index,max_index)
// void add_line(T a,T b): 直線 ax+b を追加
// void add_segment_line(T Llim,T Rlim,T a,T b): [Llim,Rlim]の線分 ax+b を追加
// T get(T x): min(ax+b) を返す

template <typename T>
struct DynamicLiChaoTree{
private:
    struct Line{
        T slope, intercept;
        Line() : slope(0), intercept(numeric_limits<T>::max() / 2) {}
        Line(T a, T b):slope(a),intercept(b) {}
        T f(T x) {return slope * x + intercept;}
    };

    struct node {
        Line line;
        node *left, *right;

        node(Line line):
            line(line),
            left(nullptr),
            right(nullptr) {}
    };

    const long long min_index,max_index;
    node* root;

    node* add_line(node* &t, long long l, long long r, Line line) const {
        if(l == r) return nullptr;
        if (!t) {
            t = new node(line);
            return t;
        }
        long long m = (l + r) >> 1;
        bool L = (line.f(l) <= t->line.f(l));
        bool M = (line.f(m) <= t->line.f(m));
        bool R = (line.f(r) <= t->line.f(r));
        if(L && R){
            t->line = line;
            return t;
        }
        if(!L && !R){
            return t;
        }
        if(M){
            swap(t->line, line);
        }
        if(L != M){
            t->left=add_line(t->left, l, m, line);
        }
        else{
            t->right=add_line(t->right, m, r, line);
        }
        return t;
    }

    node* add_segment_line(node* t, long long Llim, long long Rlim, long long l, long long r, Line line){
        if(r < Llim || Rlim < l) return t;
        if(Llim <= l && r <= Rlim) return add_line(t, l, r, line);
        if(t == nullptr) t = new node();
        long long m = (l + r) >> 1;
        t->left = add_segment_line(t, Llim, Rlim, l, m, line);
        t->right = add_segment_line(t, Llim, Rlim, m, r, line);
        return t;
    }

public:
    DynamicLiChaoTree(T min_index,T max_index):
        min_index(min_index),
        max_index(max_index),
        root(nullptr){}

    void add_line(T a, T b){
        Line line = Line{a, b};
        add_line(root, min_index, max_index, line);
    }

    void add_segment_line(T Llim, T Rlim, T a, T b){
        Line line = Line{a,b};
        add_segment_line(root, Llim, Rlim, min_index, max_index, line);
    }

    T get(long long x){
        node *t = root;
        long long l = min_index, r = max_index;
        T ret = numeric_limits<T>::max() / 2;
        while(r - l > 0 && t != nullptr){
            long long m = (l + r) >> 1;
            ret = min(ret, t->line.f(x));
            if(x < m){
                r = m;
                t = t->left;
            }
            else{
                l = m;
                t = t->right;
            }
        }
        return ret;
    }
};

void solve(){
    ll N,A,B,W;
    cin>>N>>A>>B>>W;
    vl D(N);
    cin>>D;
    vl DP(N+1);
    DP[0]=W;
    DynamicLiChaoTree<ll> LCT(0,4e5);
    for(int i=1;i<=N;i++){
        LCT.add_line(-B*(i-1),A*(i-1)+B*i*(i-1)/2+DP[i-1]);
        DP[i]=LCT.get(i)+D[i-1]-A*(i-1)+B*i*(i-1)/2;
    }
    ll ans=LINF;
    rep(i,N+1){
        chmin(ans,DP[i]-A*(N-i)+B*(N-i)*(N-i+1)/2);
    }
    cout<<ans<<endl;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();
}
0