結果

問題 No.1117 数列分割
ユーザー betrue12betrue12
提出日時 2020-07-17 22:48:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 2,420 bytes
コンパイル時間 2,596 ms
コンパイル使用メモリ 217,944 KB
実行使用メモリ 275,136 KB
最終ジャッジ日時 2023-08-20 03:56:13
合計ジャッジ時間 11,071 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 930 ms
47,772 KB
testcase_04 AC 855 ms
42,672 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 8 ms
4,760 KB
testcase_07 AC 6 ms
4,728 KB
testcase_08 AC 786 ms
57,844 KB
testcase_09 AC 185 ms
15,760 KB
testcase_10 AC 796 ms
53,172 KB
testcase_11 MLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
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 StarrySky {
    int n, n_org;
    T e;
    vector<T> dat, added;
    typedef function<T(T a, T b)> Func;
    Func f;
 
    StarrySky(){}
    StarrySky(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);
        added.assign(2*n-1, 0);
    }
 
    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 add_between(int a, int b, T x){
        add(a, b+1, x, 0, 0, n);
    }
 
    void add(int a, int b, T x, int k, int lb, int rb){
        if(b <= lb || rb <= a) return;
        if(a <= lb && rb <= b){
            added[k] += x;
        }else{
            int mb = (lb+rb)>>1;
            add(a, b, x, 2*k+1, lb, mb);
            add(a, b, x, 2*k+2, mb, rb);
            dat[k] = f(dat[2*k+1] + added[2*k+1], dat[2*k+2] + added[2*k+2]);
        }
    }
 
    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] + added[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) + added[k];
    }
};

int main(){
    int N, K, M;
    cin >> N >> K >> M;
    vector<int> A(N);
    for(int i=0; i<N; i++) cin >> A[i];
    StarrySky<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].add_between(0, 0, 1e15);

    for(int i=0; i<N; i++){
        for(int k=0; k<=K; k++) for(int l=0; l<2; l++) st[k][l].add_between(0, i, (l ? -A[i] : 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), st[k-1][1].between(L, i));
            for(int l=0; l<2; l++) st[k][l].add_between(i+1, i+1, v - st[k][l].between(i+1, i+1));
        }
    }
    cout << st[K][0].between(N, N) << endl;
    return 0;
}
0