結果

問題 No.1117 数列分割
ユーザー shinchanshinchan
提出日時 2020-06-02 00:24:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 280 ms / 3,000 ms
コード長 1,619 bytes
コンパイル時間 2,649 ms
コンパイル使用メモリ 213,440 KB
実行使用メモリ 77,952 KB
最終ジャッジ日時 2024-05-07 11:15:11
合計ジャッジ時間 5,211 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 16 ms
9,728 KB
testcase_04 AC 13 ms
8,960 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 13 ms
7,936 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 13 ms
8,064 KB
testcase_11 AC 46 ms
22,912 KB
testcase_12 AC 48 ms
24,448 KB
testcase_13 AC 71 ms
33,024 KB
testcase_14 AC 69 ms
34,048 KB
testcase_15 AC 95 ms
41,600 KB
testcase_16 AC 103 ms
45,952 KB
testcase_17 AC 12 ms
7,808 KB
testcase_18 AC 280 ms
77,952 KB
testcase_19 AC 237 ms
77,696 KB
testcase_20 AC 82 ms
39,168 KB
testcase_21 AC 234 ms
77,696 KB
testcase_22 AC 215 ms
76,544 KB
testcase_23 AC 220 ms
76,544 KB
testcase_24 AC 234 ms
75,520 KB
testcase_25 AC 217 ms
75,520 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 9 ms
6,016 KB
testcase_28 AC 9 ms
5,888 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