結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 44 ms
38,352 KB
testcase_04 AC 35 ms
37,020 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 34 ms
45,520 KB
testcase_09 AC 13 ms
25,140 KB
testcase_10 AC 34 ms
48,760 KB
testcase_11 AC 193 ms
112,600 KB
testcase_12 AC 225 ms
116,272 KB
testcase_13 AC 334 ms
109,768 KB
testcase_14 AC 361 ms
145,876 KB
testcase_15 AC 507 ms
168,564 KB
testcase_16 AC 541 ms
177,576 KB
testcase_17 AC 40 ms
82,176 KB
testcase_18 MLE -
testcase_19 MLE -
testcase_20 AC 425 ms
160,760 KB
testcase_21 MLE -
testcase_22 AC 993 ms
254,004 KB
testcase_23 AC 1,006 ms
253,828 KB
testcase_24 AC 977 ms
251,208 KB
testcase_25 AC 982 ms
250,972 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 31 ms
77,564 KB
testcase_28 AC 31 ms
77,456 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)
            );
        }
    }
    cout << dp[n][k] << endl;
}
0