結果

問題 No.505 カードの数式2
ユーザー startcppstartcpp
提出日時 2017-04-22 19:10:02
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,096 bytes
コンパイル時間 548 ms
コンパイル使用メモリ 58,380 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-28 21:24:19
合計ジャッジ時間 1,897 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 1 ms
4,380 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 2 ms
4,384 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 2 ms
4,380 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

//k項までの{最小, 最大}が分かっていれば, k+1項までの{最小, 最大}を計算できる. {}の中が同じなので, 最小と最大を持ってDPできそう。
#include <iostream>
#include <algorithm>
#define int long long
using namespace std;

int INF = 1e+16;
int n;
int a[16];
int dp[17][2];

signed main() {
	int i, j;
	
	cin >> n;
	for (i = 0; i < n; i++) cin >> a[i];
	for (i = 0; i <= n; i++) {
		dp[i][0] = INF;
		dp[i][1] = -INF;
	}
	dp[0][0] = 0;
	
	for (i = 0; i < n; i++) {
		for (j = 0; j < 2; j++) {
			if (dp[i][j] == INF || dp[i][j] == -INF) continue;
			dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + a[i]);
			dp[i + 1][1] = max(dp[i + 1][1], dp[i][j] + a[i]);
			dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] - a[i]);
			dp[i + 1][1] = max(dp[i + 1][1], dp[i][j] - a[i]);
			dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] * a[i]);
			dp[i + 1][1] = max(dp[i + 1][1], dp[i][j] * a[i]);
			if (a[i] != 0) {
				dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] / a[i]);
				dp[i + 1][1] = max(dp[i + 1][1], dp[i][j] / a[i]);
			}
		}
	}
	
	cout << dp[n][1] << endl;
	return 0;
}
0