結果
| 問題 | 
                            No.1117 数列分割
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-07-18 10:07:18 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 136 ms / 3,000 ms | 
| コード長 | 1,256 bytes | 
| コンパイル時間 | 921 ms | 
| コンパイル使用メモリ | 84,604 KB | 
| 実行使用メモリ | 69,324 KB | 
| 最終ジャッジ日時 | 2024-11-30 20:31:50 | 
| 合計ジャッジ時間 | 3,435 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 26 | 
ソースコード
#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;
	
	static int dp[MAX][MAX];
	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; 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;
}