結果

問題 No.1117 数列分割
ユーザー kyoprounokyoprouno
提出日時 2020-07-25 14:10:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 348 ms / 3,000 ms
コード長 1,971 bytes
コンパイル時間 2,526 ms
コンパイル使用メモリ 186,292 KB
実行使用メモリ 78,964 KB
最終ジャッジ日時 2023-09-09 12:04:19
合計ジャッジ時間 6,817 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 18 ms
9,840 KB
testcase_04 AC 20 ms
12,196 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 14 ms
8,324 KB
testcase_09 AC 5 ms
4,808 KB
testcase_10 AC 14 ms
8,504 KB
testcase_11 AC 56 ms
23,404 KB
testcase_12 AC 57 ms
24,812 KB
testcase_13 AC 86 ms
33,056 KB
testcase_14 AC 86 ms
34,392 KB
testcase_15 AC 116 ms
41,592 KB
testcase_16 AC 126 ms
45,996 KB
testcase_17 AC 15 ms
7,992 KB
testcase_18 AC 348 ms
78,964 KB
testcase_19 AC 297 ms
77,948 KB
testcase_20 AC 97 ms
39,492 KB
testcase_21 AC 283 ms
77,768 KB
testcase_22 AC 282 ms
76,688 KB
testcase_23 AC 274 ms
76,676 KB
testcase_24 AC 281 ms
75,632 KB
testcase_25 AC 281 ms
75,744 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 11 ms
6,024 KB
testcase_28 AC 12 ms
6,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))

using namespace std;

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;}

const ll INF=1e18;

int main(){
    ll n,k,m;
    cin >> n >> k >> m;
    vector<ll> a(n),s(n+1);
    rep(i,n){
        cin >> a[i];
    }
    rep(i,n){
        s[i+1]=s[i]+a[i];
    }
    vector<vector<ll>> dp(n+1,vector<ll>(k+1,-INF));
    vector<deque<pll>> dq1(k + 1), dq2(k + 1);
    dq1[0].emplace_back(0, 0);
    dq2[0].emplace_back(0, 0);
    dp[0][0]=0;
    for(ll i=1;i<=n;i++){
        for (int j = 1; j <= k; j++) {
            ll mx1 = -INF;
            ll mx2 = -INF;
            while (!dq1[j - 1].empty() and dq1[j - 1].front().first < i - m) dq1[j - 1].pop_front(); //biの長さはm以下
            while (!dq2[j - 1].empty() and dq2[j - 1].front().first < i - m) dq2[j - 1].pop_front(); //biの長さはm以下
            if (!dq1[j - 1].empty()) mx1 = dq1[j - 1].front().second;
            if (!dq2[j - 1].empty()) mx2 = dq2[j - 1].front().second;
            chmax(dp[i][j], mx1 - s[i]);
            chmax(dp[i][j], mx2 + s[i]);
        }
        for (int j = 0; j <= k; j++) {
            while (!dq1[j].empty() and dq1[j].back().second < dp[i][j] + s[i]) dq1[j].pop_back();
            while (!dq2[j].empty() and dq2[j].back().second < dp[i][j] - s[i]) dq2[j].pop_back();
            dq1[j].emplace_back(i, dp[i][j] + s[i]);
            dq2[j].emplace_back(i, dp[i][j] - s[i]);
        }
    }
    cout << dp[n][k] << endl;
    return 0;
}
0