結果

問題 No.1117 数列分割
ユーザー betrue12betrue12
提出日時 2020-07-17 23:02:41
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,370 ms / 3,000 ms
コード長 2,107 bytes
コンパイル時間 2,419 ms
コンパイル使用メモリ 214,108 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-20 04:03:43
合計ジャッジ時間 26,644 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 191 ms
4,380 KB
testcase_04 AC 174 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 3 ms
4,384 KB
testcase_07 AC 2 ms
4,504 KB
testcase_08 AC 132 ms
4,384 KB
testcase_09 AC 44 ms
4,380 KB
testcase_10 AC 119 ms
4,384 KB
testcase_11 AC 589 ms
4,384 KB
testcase_12 AC 890 ms
4,380 KB
testcase_13 AC 808 ms
4,384 KB
testcase_14 AC 1,014 ms
4,384 KB
testcase_15 AC 1,611 ms
4,380 KB
testcase_16 AC 1,848 ms
4,380 KB
testcase_17 AC 133 ms
4,384 KB
testcase_18 AC 1,997 ms
4,380 KB
testcase_19 AC 2,370 ms
4,380 KB
testcase_20 AC 1,401 ms
4,380 KB
testcase_21 AC 1,905 ms
4,380 KB
testcase_22 AC 1,909 ms
4,380 KB
testcase_23 AC 1,920 ms
4,380 KB
testcase_24 AC 1,880 ms
4,380 KB
testcase_25 AC 1,903 ms
4,380 KB
testcase_26 AC 1 ms
4,384 KB
testcase_27 AC 93 ms
4,384 KB
testcase_28 AC 92 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<typename T>
struct Segtree {
    int n, n_org;
    T e;
    vector<T> dat;
    typedef function<T(T a, T b)> Func;
    Func f;

    Segtree(){}
    Segtree(int n_input, Func f_input, T e_input){
        initialize(n_input, f_input, e_input);
    }
    void initialize(int n_input, Func f_input, T e_input){
        n_org = n_input;
        f = f_input;
        e = e_input;
        n = 1;
        while(n < n_input) n <<= 1;
        dat.assign(2*n-1, e);
    }

    void build(vector<T>& A){
        for(int k=0; k<int(A.size()); k++) dat[k+n-1] = A[k];
        for(int k=n-2; k>=0; k--) dat[k] = f(dat[2*k+1], dat[2*k+2]);
    }

    void update(int k, T a){
        k += n - 1;
        dat[k] = a;
        while(k > 0){
            k = (k - 1)/2;
            dat[k] = f(dat[2*k+1], dat[2*k+2]);
        }
    }

    T get(int k){
        return dat[k+n-1];
    }

    T between(int a, int b){
        return query(a, b+1, 0, 0, n);
    }

    T query(int a, int b, int k, int l, int r){
        if(r<=a || b<=l) return e;
        if(a<=l && r<=b) return dat[k];
        T vl = query(a, b, 2*k+1, l, (l+r)/2);
        T vr = query(a, b, 2*k+2, (l+r)/2, r);
        return f(vl, vr);
    }
};

int main(){
    int N, K, M;
    cin >> N >> K >> M;
    vector<int> A(N);
    for(int i=0; i<N; i++) cin >> A[i];
    Segtree<int64_t> st[2];
    vector<int64_t> init(N+1, -1e15);
    init[0] = 0;
    for(int l=0; l<2; l++){
        st[l].initialize(N+1, [](auto a, auto b){ return max(a, b); }, -1e18);
        st[l].build(init);
    }

    for(int k=1; k<=K; k++){
        int64_t S = 0;
        vector<int64_t> V0(N+1, -1e18);
        auto V1 = V0;
        for(int i=0; i<N; i++){
            S += A[i];
            int L = max(i-M+1, 0);
            int64_t v = max(st[0].between(L, i) + S, st[1].between(L, i) - S);
            V0[i+1] = v-S;
            V1[i+1] = v+S;
        }
        st[0].build(V0);
        st[1].build(V1);
    }

    int64_t S = accumulate(A.begin(), A.end(), 0LL);
    cout << st[0].between(N, N) + S << endl;
    return 0;
}
0