結果
問題 | No.1117 数列分割 |
ユーザー |
|
提出日時 | 2020-07-19 21:32:20 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 241 ms / 3,000 ms |
コード長 | 2,967 bytes |
コンパイル時間 | 1,625 ms |
コンパイル使用メモリ | 176,324 KB |
実行使用メモリ | 74,288 KB |
最終ジャッジ日時 | 2024-12-17 14:03:10 |
合計ジャッジ時間 | 5,302 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 26 |
ソースコード
#include <bits/stdc++.h>using namespace std;typedef long long ll;#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)#define ALL(v) (v).begin(),(v).end()#define CLR(t,v) memset(t,(v),sizeof(t))template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}ll nextLong() { ll x; scanf("%lld", &x); return x;}// スライド最小値// 蟻本のアルゴリズム。// 幅k=10の最小値を求めるとして、1,3,6,2,4,... という入力数列を左から順に見ていったときに、// 2に着いた時点で3,6は捨てても構わないよねということを利用してdequeでがんばる//// K : 区間の幅// push(val, pos) : val 区間の右端が pos になるように移動させる。// get(pos) : 区間の右端が pos になるように移動させて、そのときの最小値を返す。// get(pos, &isEmpty) : deque が空のときには isEmpty に true がセットされ、無効な値が返される。//// push/get で指定する pos は時間とともに単調増加すること。//struct SlideMin {int K;deque<ll> idx, data;SlideMin(int K) {this->K = K;}void move(int pos) {while (!idx.empty() && pos - idx.front() >= K) {idx.pop_front();data.pop_front();}}void push(ll val, int pos) {move(pos);while (!data.empty() && data.back() >= val) { // min: >=, max: <=idx.pop_back();data.pop_back();}idx.push_back(pos);data.push_back(val);}ll get(int pos) {bool dummy;return get(pos, dummy);}ll get(int pos, bool& empty) { // pos: right edge of the window (closed interval).move(pos);empty = data.empty();if (empty) return 0; // return value is meaningless.return data.front();}};const ll INF = 1LL << 61;ll dp[3005][3005];int main2() {REP(i, 3005) REP(j, 3005) dp[i][j] = -INF;int N = nextLong();int K = nextLong();int M = nextLong();vector<ll> A(N), S(N+1);REP(i, N) A[i] = nextLong();S[0] = 0;REP(i, N) S[i+1] = S[i] + A[i];// pv(ALL(A));// pv(ALL(S));dp[0][0] = 0;for (int k = 1; k <= K; k++) {vector<ll> X(N+1), Y(N+1);for (int i = 0; i <= N; i++) {X[i] = dp[k-1][i] - S[i];Y[i] = dp[k-1][i] + S[i];}SlideMin sX(M), sY(M);for (int n = 1; n <= N; n++) {sX.push(-X[n-1], n-1);sY.push(-Y[n-1], n-1);ll val1 = -sX.get(n-1);ll val2 = -sY.get(n-1);ll val = max(val1 + S[n], val2 - S[n]);dp[k][n] = val;}// pv(dp[k], dp[k]+N+1);}ll ans = dp[K][N];cout << ans << endl;return 0;}int main() {#ifdef LOCALfor (;!cin.eof();cin>>ws)#endifmain2();return 0;}