結果

問題 No.1117 数列分割
ユーザー betrue12betrue12
提出日時 2020-07-17 22:54:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 2,886 bytes
コンパイル時間 2,538 ms
コンパイル使用メモリ 213,848 KB
実行使用メモリ 271,400 KB
最終ジャッジ日時 2023-08-20 04:00:05
合計ジャッジ時間 16,013 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 309 ms
25,696 KB
testcase_04 AC 276 ms
23,112 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 3 ms
4,388 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 244 ms
30,632 KB
testcase_09 AC 61 ms
9,564 KB
testcase_10 AC 225 ms
28,420 KB
testcase_11 AC 1,366 ms
137,148 KB
testcase_12 AC 1,758 ms
158,132 KB
testcase_13 AC 1,830 ms
120,096 KB
testcase_14 AC 2,311 ms
192,852 KB
testcase_15 MLE -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
権限があれば一括ダウンロードができます

ソースコード

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);
    }

    // [S, t] が条件checkを満たす最大のtを求める
    int bisect(int S, function<bool(T a)> check){
        T val = get(S);
        int k = S+n-1, l = S, r = S+1;
        while(true){
            while(k%2) k = (k-1)/2, r += r-l;
            T val2 = f(val, dat[k]);
            if(check(val2)){
                if(r == n) return n_org-1;
                val = val2;
                k++;
                int d = r-l;
                l += d, r += d;
            }else{
                break;
            }
        }
        while(k<n-1){
            T val2 = f(val, dat[2*k+1]);
            if(check(val2)){
                val = val2;
                k = 2*k+2;
                l = (l+r)/2;
            }else{
                k = 2*k+1;
                r = (l+r)/2;
            }
        }
        return min(l, n_org)-1;
    }
};

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[3001][2];
    vector<int64_t> init(N+1, -1e15);
    for(int k=0; k<=K; k++) for(int l=0; l<2; l++){
        st[k][l].initialize(N+1, [](auto a, auto b){ return max(a, b); }, -1e18);
        st[k][l].build(init);
    }
    for(int l=0; l<2; l++) st[0][l].update(0, 0);
    
    int64_t S = 0;

    for(int i=0; i<N; i++){
        S += A[i];
        int L = max(i-M+1, 0);
        for(int k=1; k<=K; k++){
            int64_t v = max(st[k-1][0].between(L, i) + S, st[k-1][1].between(L, i) - S);
            st[k][0].update(i+1, v - S);
            st[k][1].update(i+1, v + S);
        }
    }
    cout << st[K][0].between(N, N) + S << endl;
    return 0;
}
0