結果

問題 No.1117 数列分割
ユーザー milanis48663220milanis48663220
提出日時 2021-05-19 01:44:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 2,550 bytes
コンパイル時間 1,426 ms
コンパイル使用メモリ 128,112 KB
実行使用メモリ 256,640 KB
最終ジャッジ日時 2024-04-17 09:18:30
合計ジャッジ時間 12,242 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 46 ms
24,576 KB
testcase_04 AC 38 ms
22,656 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 38 ms
25,472 KB
testcase_09 AC 13 ms
10,752 KB
testcase_10 AC 38 ms
25,728 KB
testcase_11 AC 191 ms
87,936 KB
testcase_12 AC 211 ms
96,128 KB
testcase_13 AC 320 ms
99,200 KB
testcase_14 AC 343 ms
126,464 KB
testcase_15 AC 479 ms
160,512 KB
testcase_16 AC 513 ms
160,512 KB
testcase_17 AC 43 ms
30,080 KB
testcase_18 MLE -
testcase_19 MLE -
testcase_20 AC 405 ms
138,524 KB
testcase_21 MLE -
testcase_22 AC 916 ms
254,064 KB
testcase_23 AC 946 ms
253,744 KB
testcase_24 AC 926 ms
251,320 KB
testcase_25 AC 947 ms
251,136 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 29 ms
27,040 KB
testcase_28 AC 29 ms
23,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#include <deque>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template <class T>
class SlidingMaximum{
    public:
    deque<T> dq;
    vector<T> a;

    void push_right(int r, T x){
        assert(r == a.size());
        a.push_back(x);
        while(!dq.empty() && a[dq.back()] <= x) dq.pop_back();
        dq.push_back(r);
    }

    void pop_left(int l){
        while(!dq.empty() && dq.front() <= l) dq.pop_front();
    }

    T query(){ 
        return a[dq.front()];
    }
};

const ll INF = 1e18;
ll dp[3005][3005];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, k, m; cin >> n >> k >> m;
    vector<ll> a(n+1), a_cumsum(n+2);
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        a_cumsum[i+1] = a_cumsum[i]+a[i];
    }
    for(int i = 0; i <= n; i++){
        for(int j = 0; j <= k; j++){
            dp[i][j] = -INF;
        }
    }
    dp[0][0] = 0;
    vector<SlidingMaximum<ll>> sm_pos(k+1, SlidingMaximum<ll>());
    vector<SlidingMaximum<ll>> sm_neg(k+1, SlidingMaximum<ll>());
    auto sect_sum = [&](int l, int r){ return a_cumsum[r]-a_cumsum[l]; };
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= k; j++){
            // [max(0, i-m), i)
            int l = max(0, i-m), r = i-1;
            // cout << dp[r][j-1] << endl;
            sm_pos[j-1].push_right(r, dp[r][j-1]+sect_sum(r+1, n+1));
            sm_pos[j-1].pop_left(l-1);
            sm_neg[j-1].push_right(r, dp[r][j-1]-sect_sum(r+1, n+1));
            sm_neg[j-1].pop_left(l-1);
            dp[i][j] = max(
                sm_pos[j-1].query()-sect_sum(i+1, n+1),
                sm_neg[j-1].query()+sect_sum(i+1, n+1)
            );
            // for(int l = max(0, i-m); l < i; l++){
            //     chmax(dp[i][j], dp[l][j-1]+sect_sum(l+1, i+1));
            //     chmax(dp[i][j], dp[l][j-1]-sect_sum(l+1, i+1));
            // }
        }
    }
    cout << dp[n][k] << endl;
}
0