結果

問題 No.1117 数列分割
ユーザー milanis48663220
提出日時 2021-05-19 02:16:32
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,073 ms / 3,000 ms
コード長 2,371 bytes
コンパイル時間 1,464 ms
コンパイル使用メモリ 124,808 KB
最終ジャッジ日時 2025-01-21 13:50:17
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#include <deque>
#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;
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; }
using namespace std;
typedef long long ll;
template <class T>
class SlidingMaximum{
public:
deque<T> dq;
vector<T> a;
SlidingMaximum(int sz){
a.resize(sz);
}
void push_right(int r, T x){
a[r] = x;
while(!dq.empty() && a[dq.back()] <= x) dq.pop_back();
dq.push_back(r);
}
void pop_left(int l){
while(!dq.empty() && dq.front() <= l) dq.pop_front();
}
T query(){
return a[dq.front()];
}
};
const ll INF = 1e18;
ll dp[3001][3001];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
int n, k, m; cin >> n >> k >> m;
vector<ll> a(n+1), a_cumsum(n+2);
for(int i = 1; i <= n; i++) {
cin >> a[i];
a_cumsum[i+1] = a_cumsum[i]+a[i];
}
for(int i = 0; i <= n; i++){
for(int j = 0; j <= k; j++){
dp[i][j] = -INF;
}
}
dp[0][0] = 0;
vector<SlidingMaximum<ll>> sm_pos(k+1, SlidingMaximum<ll>(n+1));
vector<SlidingMaximum<ll>> sm_neg(k+1, SlidingMaximum<ll>(n+1));
auto sect_sum = [&](int l, int r){ return a_cumsum[r]-a_cumsum[l]; };
for(int i = 1; i <= n; i++){
for(int j = 1; j <= k; j++){
// [max(0, i-m), i)
int l = max(0, i-m), r = i-1;
// cout << dp[r][j-1] << endl;
sm_pos[j-1].push_right(r, dp[r][j-1]+sect_sum(r+1, n+1));
sm_pos[j-1].pop_left(l-1);
sm_neg[j-1].push_right(r, dp[r][j-1]-sect_sum(r+1, n+1));
sm_neg[j-1].pop_left(l-1);
dp[i][j] = max(
sm_pos[j-1].query()-sect_sum(i+1, n+1),
sm_neg[j-1].query()+sect_sum(i+1, n+1)
);
}
}
cout << dp[n][k] << endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0