結果
問題 |
No.1117 数列分割
|
ユーザー |
![]() |
提出日時 | 2020-08-02 11:30:04 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 505 ms / 3,000 ms |
コード長 | 2,350 bytes |
コンパイル時間 | 3,754 ms |
コンパイル使用メモリ | 203,364 KB |
最終ジャッジ日時 | 2025-01-12 13:13:09 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 26 |
ソースコード
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<long long> VL; typedef vector<vector<long long>> VVL; typedef vector<string> VS; typedef pair<ll,ll> P; typedef tuple<int,int,int> tpl; #define ALL(a) (a).begin(),(a).end() #define SORT(c) sort((c).begin(),(c).end()) #define REVERSE(c) reverse((c).begin(),(c).end()) #define EXIST(m,v) (m).find((v)) != (m).end() #define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin() #define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin() #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i) #define RREP(i,n) RFOR(i,n,0) #define en "\n" constexpr double EPS = 1e-9; constexpr double PI = 3.1415926535897932; constexpr int INF = 2147483647; constexpr long long LINF = 1LL<<60; constexpr long long MOD = 1000000007; // 998244353; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } void Main(){ ll N,K,M; cin >> N >> K >> M; VL A(N); REP(i,N) cin >> A[i]; VL S(N+1,0); REP(i,N) S[i+1] += S[i]+A[i]; ll dp[N+1][K+1]; REP(i,N+1)REP(j,K+1) dp[i][j] = -LINF; deque<P> deq1[K+1],deq2[K+1]; dp[0][0] = 0; deq1[0].emplace_back(0,0); deq2[0].emplace_back(0,0); REP(i,N)REP(j,min((ll)i+1,K)){ while(!deq1[j].empty() && deq1[j].front().first<i+1-M) deq1[j].pop_front(); while(!deq2[j].empty() && deq2[j].front().first<i+1-M) deq2[j].pop_front(); if(!deq1[j].empty()) chmax(dp[i+1][j+1], deq1[j].front().second + S[i+1]); if(!deq2[j].empty()) chmax(dp[i+1][j+1], deq2[j].front().second - S[i+1]); while(!deq1[j+1].empty() && deq1[j+1].back().second<=dp[i+1][j+1]-S[i+1]) deq1[j+1].pop_back(); while(!deq2[j+1].empty() && deq2[j+1].back().second<=dp[i+1][j+1]+S[i+1]) deq2[j+1].pop_back(); deq1[j+1].emplace_back(i+1,dp[i+1][j+1]-S[i+1]); deq2[j+1].emplace_back(i+1,dp[i+1][j+1]+S[i+1]); } cout << dp[N][K] << en; return; } int main(void){ cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15); int t=1; //cin>>t; REP(_,t) Main(); return 0; }