結果

問題 No.1117 数列分割
ユーザー okok
提出日時 2020-07-18 10:03:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 188 ms / 3,000 ms
コード長 1,339 bytes
コンパイル時間 1,477 ms
コンパイル使用メモリ 96,388 KB
実行使用メモリ 73,856 KB
最終ジャッジ日時 2024-05-07 23:31:18
合計ジャッジ時間 3,719 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 13 ms
8,832 KB
testcase_04 AC 9 ms
8,064 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 13 ms
7,424 KB
testcase_09 AC 5 ms
5,376 KB
testcase_10 AC 14 ms
7,552 KB
testcase_11 AC 65 ms
21,632 KB
testcase_12 AC 59 ms
23,040 KB
testcase_13 AC 62 ms
30,720 KB
testcase_14 AC 94 ms
32,256 KB
testcase_15 AC 85 ms
39,040 KB
testcase_16 AC 137 ms
43,776 KB
testcase_17 AC 17 ms
7,552 KB
testcase_18 AC 188 ms
73,856 KB
testcase_19 AC 179 ms
73,856 KB
testcase_20 AC 121 ms
37,248 KB
testcase_21 AC 172 ms
73,856 KB
testcase_22 AC 170 ms
72,704 KB
testcase_23 AC 178 ms
72,832 KB
testcase_24 AC 168 ms
71,808 KB
testcase_25 AC 174 ms
71,552 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 9 ms
6,016 KB
testcase_28 AC 9 ms
6,016 KB
権限があれば一括ダウンロードができます

ソースコード

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);
	
	int N, K, M;
	vector<int> A;
	vector<int> sum;
	vector<vector<int>> dp;
	vector<pair<int,int>> q, q2;
	cin>>N>>K>>M;
	
	A.resize(N);
	dp.resize(N+1, vector<int>(K+1, -INF));
	sum.resize(N+1);
	
	q.resize(N+2);
	q2.resize(N+2);
	
	for(int i = 0; i < N; i++){
		cin>>A[i];
	
		sum[i+1] = sum[i] + A[i];
		
		if(i < M) dp[i][1] = abs(sum[i+1]);
	}

	for(int j = 2; j <= K; j++){
		int f = 0, e = 0, f2 = 0, e2 = 0;
		
		for(int i = j-1; i < N; i++){
			int num = dp[i-1][j-1] - sum[i];
			int num2 = dp[i-1][j-1] + 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][j] = max(q[f].second + sum[i+1], q2[f2].second - sum[i+1]);
		}
	}
	
	cout<<dp[N-1][K]<<endl;
	
	return 0;
}
0