結果

問題 No.1117 数列分割
ユーザー shinchanshinchan
提出日時 2020-06-02 00:24:18
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 307 ms / 3,000 ms
コード長 1,619 bytes
コンパイル時間 2,339 ms
コンパイル使用メモリ 212,204 KB
実行使用メモリ 77,740 KB
最終ジャッジ日時 2023-08-20 03:40:00
合計ジャッジ時間 6,037 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 17 ms
9,956 KB
testcase_04 AC 15 ms
9,032 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 14 ms
7,708 KB
testcase_09 AC 5 ms
4,908 KB
testcase_10 AC 14 ms
7,748 KB
testcase_11 AC 54 ms
22,840 KB
testcase_12 AC 57 ms
24,352 KB
testcase_13 AC 85 ms
32,808 KB
testcase_14 AC 82 ms
34,012 KB
testcase_15 AC 108 ms
41,716 KB
testcase_16 AC 119 ms
45,720 KB
testcase_17 AC 14 ms
7,780 KB
testcase_18 AC 307 ms
77,740 KB
testcase_19 AC 255 ms
77,740 KB
testcase_20 AC 95 ms
39,072 KB
testcase_21 AC 246 ms
77,500 KB
testcase_22 AC 234 ms
76,380 KB
testcase_23 AC 236 ms
76,388 KB
testcase_24 AC 236 ms
75,428 KB
testcase_25 AC 232 ms
75,284 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 10 ms
6,076 KB
testcase_28 AC 12 ms
6,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod=1000000007, INF=(1LL<<60);
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;

void chmax(ll &a, ll b){a = max(a, b);}
int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    ll n, k, m;
    cin >> n >> k >> m;
    vector<ll> a(n+1,0);
    for(int i=0;i<n;i++){
    	cin >> a[i];
    }
    for(int i = n - 1;i>=0;i--){
    	a[i] += a[i+1];
    }
    vector<deque<pair<int, ll> > > dqp(k+1), dqm(k+1);
    dqp[0].push_back({-1, a[0]});
    dqm[0].push_back({-1, -a[0]});
    vector<vector<ll> > dp(n+1, vector<ll> (k+1, 0));
    for(int i=0;i<n;i++){
    	for(int j=0;j<=k;j++) dp[i+1][j] = 0;
    	for(int j=k-1;j>=0;j--){
    		while(!dqp[j].empty() && dqp[j].front().first < i-m) dqp[j].pop_front();
    		while(!dqm[j].empty() && dqm[j].front().first < i-m) dqm[j].pop_front();
    		if(!dqp[j].empty()) chmax(dp[i+1][j+1], dqp[j].front().second - a[i + 1]);
    		if(!dqm[j].empty()) chmax(dp[i+1][j+1], dqm[j].front().second + a[i + 1]);
    		if (j + 1 < k) {
                while (!dqp[j + 1].empty() && dqp[j + 1].back().second <= dp[i + 1][j + 1] + a[i + 1]) dqp[j + 1].pop_back();
                while (!dqm[j + 1].empty() && dqm[j + 1].back().second <= dp[i + 1][j + 1] - a[i + 1]) dqm[j + 1].pop_back();
                dqp[j + 1].emplace_back(i, dp[i + 1][j + 1] + a[i + 1]);
                dqm[j + 1].emplace_back(i, dp[i + 1][j + 1] - a[i + 1]);
            }
    	}
    }
    cout << dp[n][k] << endl;
    return 0;
}
0