結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 43 ms
20,480 KB
testcase_04 AC 32 ms
18,432 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 32 ms
20,736 KB
testcase_09 AC 10 ms
7,680 KB
testcase_10 AC 32 ms
19,968 KB
testcase_11 AC 196 ms
78,976 KB
testcase_12 AC 223 ms
87,936 KB
testcase_13 AC 348 ms
91,520 KB
testcase_14 AC 360 ms
116,352 KB
testcase_15 AC 501 ms
151,680 KB
testcase_16 AC 523 ms
148,352 KB
testcase_17 AC 31 ms
17,920 KB
testcase_18 MLE -
testcase_19 MLE -
testcase_20 AC 428 ms
125,184 KB
testcase_21 MLE -
testcase_22 AC 1,057 ms
252,928 KB
testcase_23 AC 1,025 ms
252,928 KB
testcase_24 AC 1,052 ms
249,088 KB
testcase_25 AC 1,004 ms
248,832 KB
testcase_26 AC 1 ms
6,940 KB
testcase_27 AC 20 ms
11,904 KB
testcase_28 AC 20 ms
11,904 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;

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];
    }
    vector<vector<ll>> dp(n+1, vector<ll>(k+1, -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)
            );
        }
    }
    cout << dp[n][k] << endl;
}
0