結果

問題 No.1117 数列分割
ユーザー milanis48663220milanis48663220
提出日時 2021-05-19 02:16:32
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 952 ms / 3,000 ms
コード長 2,371 bytes
コンパイル時間 1,318 ms
コンパイル使用メモリ 129,620 KB
実行使用メモリ 218,696 KB
最終ジャッジ日時 2024-04-17 09:32:39
合計ジャッジ時間 10,760 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 43 ms
38,040 KB
testcase_04 AC 38 ms
36,732 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 31 ms
41,500 KB
testcase_09 AC 10 ms
24,740 KB
testcase_10 AC 33 ms
47,352 KB
testcase_11 AC 178 ms
94,796 KB
testcase_12 AC 198 ms
92,424 KB
testcase_13 AC 310 ms
106,932 KB
testcase_14 AC 314 ms
121,320 KB
testcase_15 AC 416 ms
129,328 KB
testcase_16 AC 496 ms
155,720 KB
testcase_17 AC 38 ms
79,976 KB
testcase_18 AC 952 ms
218,628 KB
testcase_19 AC 874 ms
218,696 KB
testcase_20 AC 381 ms
142,604 KB
testcase_21 AC 846 ms
218,564 KB
testcase_22 AC 897 ms
216,480 KB
testcase_23 AC 836 ms
216,432 KB
testcase_24 AC 839 ms
214,156 KB
testcase_25 AC 847 ms
214,036 KB
testcase_26 AC 1 ms
6,940 KB
testcase_27 AC 29 ms
76,552 KB
testcase_28 AC 30 ms
76,752 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;
    SlidingMaximum(int sz){
        a.resize(sz);
    }

    void push_right(int r, T x){
        a[r] = 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[3001][3001];

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>(n+1));
    vector<SlidingMaximum<ll>> sm_neg(k+1, SlidingMaximum<ll>(n+1));
    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