結果
問題 | No.1117 数列分割 |
ユーザー |
![]() |
提出日時 | 2020-07-25 14:10:12 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 354 ms / 3,000 ms |
コード長 | 1,971 bytes |
コンパイル時間 | 2,230 ms |
コンパイル使用メモリ | 189,512 KB |
実行使用メモリ | 79,264 KB |
最終ジャッジ日時 | 2024-06-27 05:09:46 |
合計ジャッジ時間 | 6,165 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 26 |
ソースコード
#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;}