結果

問題 No.1117 数列分割
ユーザー ok
提出日時 2020-07-18 10:10:21
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 72 ms / 3,000 ms
コード長 1,262 bytes
コンパイル時間 705 ms
コンパイル使用メモリ 84,724 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-30 20:31:52
合計ジャッジ時間 2,440 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>
#include<queue>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;


signed main(){
	cout<<fixed<<setprecision(10);
	
	constexpr int MAX = 3010;
	int N, K, M, A;
	
	int dp[MAX][2];
	int sum[MAX];
	pair<int,int> q[MAX], q2[MAX];
	
	cin>>N>>K>>M;
	
	sum[0] = 0;
	
	for(int i = 0; i < N; i++){
		cin>>A;
	
		sum[i+1] = sum[i] + A;
		
		if(i < M) dp[i][1] = abs(sum[i+1]);
	}

	for(int j = 2, k = 0; j <= K; j++, k = !k){
		int f = 0, e = 0, f2 = 0, e2 = 0;
		
		for(int i = j-1; i < N; i++){
			int num = dp[i-1][!k] - sum[i];
			int num2 = dp[i-1][!k] + sum[i];
			
			while(e - f && q[e-1].second < num) e--;
			q[e] = {i,num}; e++;
			while(e - f && q[f].first < i - M + 1) f++;
			
			while(e2 - f2 && q2[e2-1].second < num2) e2--;
			q2[e2] = {i,num2}; e2++;
			while(e2 - f2  && q2[f2].first < i - M + 1) f2++;
			
			dp[i][k] = max(q[f].second + sum[i+1], q2[f2].second - sum[i+1]);
		}
	}
	
	cout<<dp[N-1][K%2]<<endl;
	
	return 0;
}
0