結果

問題 No.1536 仕切り直し
ユーザー spipipikespipipike
提出日時 2021-06-06 20:03:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 841 bytes
コンパイル時間 1,638 ms
コンパイル使用メモリ 174,056 KB
実行使用メモリ 48,040 KB
最終ジャッジ日時 2023-08-16 17:00:11
合計ジャッジ時間 2,862 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 4 ms
5,020 KB
testcase_04 AC 17 ms
16,748 KB
testcase_05 AC 33 ms
33,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,712 KB
testcase_08 AC 14 ms
14,784 KB
testcase_09 AC 18 ms
18,992 KB
testcase_10 AC 48 ms
48,040 KB
testcase_11 AC 10 ms
10,384 KB
testcase_12 AC 35 ms
33,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using LI = pair<ll,int>;

int main(){
	int n, m;
	cin>>n>>m;

	vector<vector<LI>> dp(n+1, vector<LI>(m+1, LI(LLONG_MIN/3, -1)));
	dp[0][0]=LI(0, -1);
	auto udp=[](LI &a, LI b){ a=max(a, b); };
	rep(i, n){
		int a;
		cin>>a;

		rep(j, m+1) udp(dp[i+1][j], LI(dp[i][j].first+(j%2==0?a:(-a)), j));
		rep(j, m) udp(dp[i+1][j+1], LI(dp[i][j].first+((j+1)%2==0?a:(-a)), j));
	}

	LI mx(LLONG_MIN, -1);
	rep(j, m+1) mx=max(mx, LI(dp[n][j].first, j));

	int j=mx.second;
	vector<int> ans;
	for(int i=n; i>=1; i--){
		if(j!=dp[i][j].second){
			j=dp[i][j].second;
			ans.push_back(i-1);
			if(j==0) break;
		}
	}

	reverse(ans.begin(), ans.end());

	while(ans.size()<m) ans.push_back(n);

	for(int e : ans) cout<<e<<endl;
	return 0;
}
0