結果

問題 No.1117 数列分割
ユーザー okok
提出日時 2020-07-18 09:17:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 268 ms / 3,000 ms
コード長 1,368 bytes
コンパイル時間 1,123 ms
コンパイル使用メモリ 97,388 KB
実行使用メモリ 74,004 KB
最終ジャッジ日時 2023-08-20 17:02:08
合計ジャッジ時間 4,997 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 16 ms
8,616 KB
testcase_04 AC 13 ms
8,300 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 16 ms
7,652 KB
testcase_09 AC 6 ms
4,628 KB
testcase_10 AC 17 ms
7,716 KB
testcase_11 AC 82 ms
21,804 KB
testcase_12 AC 77 ms
22,912 KB
testcase_13 AC 79 ms
30,812 KB
testcase_14 AC 122 ms
32,208 KB
testcase_15 AC 110 ms
38,712 KB
testcase_16 AC 176 ms
43,520 KB
testcase_17 AC 20 ms
7,692 KB
testcase_18 AC 253 ms
73,892 KB
testcase_19 AC 268 ms
73,948 KB
testcase_20 AC 160 ms
37,444 KB
testcase_21 AC 232 ms
74,004 KB
testcase_22 AC 218 ms
72,532 KB
testcase_23 AC 232 ms
72,500 KB
testcase_24 AC 228 ms
71,932 KB
testcase_25 AC 230 ms
71,372 KB
testcase_26 AC 2 ms
4,384 KB
testcase_27 AC 12 ms
5,960 KB
testcase_28 AC 11 ms
6,084 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<deque<int>> q, q2;
	cin>>N>>K>>M;
	
	A.resize(N);
	dp.resize(N+1, vector<int>(K+1, -INF));
	sum.resize(N+1);
	
	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++){
		deque<pair<int,int>> q, q2;
		
		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(q.size() && q.back().second < num) q.pop_back();
			q.emplace_back(i, num);
			while(q.size() && q.front().first < i - M + 1) q.pop_front();
			
			while(q2.size() && q2.back().second < num2) q2.pop_back();
			q2.emplace_back(i, num2);
			while(q2.size() && q2.front().first < i - M + 1) q2.pop_front();
			
			dp[i][j] = max(q.front().second + sum[i+1], q2.front().second - sum[i+1]);
		}
	}

	
	cout<<dp[N-1][K]<<endl;
	
	return 0;
}
0