結果

問題 No.1117 数列分割
ユーザー leaf_1415leaf_1415
提出日時 2020-07-19 01:41:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2,154 ms / 3,000 ms
コード長 2,037 bytes
コンパイル時間 1,874 ms
コンパイル使用メモリ 94,908 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-21 11:57:23
合計ジャッジ時間 24,058 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 183 ms
4,376 KB
testcase_04 AC 171 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 124 ms
4,380 KB
testcase_09 AC 43 ms
4,376 KB
testcase_10 AC 116 ms
4,376 KB
testcase_11 AC 515 ms
4,376 KB
testcase_12 AC 760 ms
4,380 KB
testcase_13 AC 784 ms
4,380 KB
testcase_14 AC 876 ms
4,376 KB
testcase_15 AC 1,410 ms
4,376 KB
testcase_16 AC 1,555 ms
4,380 KB
testcase_17 AC 118 ms
4,376 KB
testcase_18 AC 1,916 ms
4,380 KB
testcase_19 AC 2,154 ms
4,376 KB
testcase_20 AC 1,185 ms
4,380 KB
testcase_21 AC 1,729 ms
4,376 KB
testcase_22 AC 1,727 ms
4,384 KB
testcase_23 AC 1,737 ms
4,376 KB
testcase_24 AC 1,701 ms
4,380 KB
testcase_25 AC 1,718 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 83 ms
4,376 KB
testcase_28 AC 82 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define mod 998244353

using namespace std;
typedef pair<llint, llint> P;

struct SegTree{
	int size;
	vector<llint> seg;
	
	SegTree(){}
	SegTree(int size){
		this->size = size;
		seg.resize(1<<(size+1));
	}
	
	void init()
	{
		for(int i = 0; i < (1<<(size+1)); i++) seg[i] = -inf;
	}
	
	void update(int i, llint val)
	{
		i += (1 << size);
		seg[i] = val;
		while(i > 1){
			i /= 2;
			seg[i] = max(seg[i*2], seg[i*2+1]);
		}
	}

	llint query(int a, int b, int k, int l, int r)
	{
		if(b < l || r < a) return -inf;
		if(a <= l && r <= b) return seg[k];
		llint lval = query(a, b, k*2, l, (l+r)/2);
		llint rval = query(a, b, k*2+1, (l+r)/2+1, r);
		return max(lval, rval);
	}
	llint query(int a, int b)
	{
		return query(a, b, 1, 0, (1<<size)-1);
	}
};

llint n, k, m;
SegTree seg[2], seg2[2];
llint a[3005], sum[3005];

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> k >> m;
	for(int i = 1; i <= n; i++) cin >> a[i];
	for(int i = 1; i <= n; i++) sum[i] = sum[i-1] + a[i];
	
	for(int i = 0; i < 2; i++){
		seg[i] = SegTree(12), seg[i].init();
		seg2[i] = SegTree(12), seg2[i].init();
	}
	
	seg[0].update(0, 0), seg2[0].update(0, 0);
	for(int i = 1; i <= k; i++){
		seg[i%2].init();
		for(int j = 1; j <= n; j++){
			llint tmp = max(sum[j]+seg[(i-1)%2].query(max(0LL, j-m), j-1), -sum[j]+seg2[(i-1)%2].query(max(0LL, j-m), j-1));
			seg[i%2].update(j, tmp-sum[j]);
			seg2[i%2].update(j, tmp+sum[j]);
		}
	}
	cout << seg[k%2].query(n, n) + sum[n] << endl;
	
	return 0;
}
0