結果

問題 No.10 +か×か
ユーザー masamasa
提出日時 2015-01-19 16:26:36
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 1,160 bytes
コンパイル時間 649 ms
コンパイル使用メモリ 65,044 KB
実行使用メモリ 8,356 KB
最終ジャッジ日時 2023-09-04 21:38:10
合計ジャッジ時間 1,728 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,292 KB
testcase_01 AC 4 ms
8,304 KB
testcase_02 AC 3 ms
8,356 KB
testcase_03 RE -
testcase_04 AC 8 ms
8,236 KB
testcase_05 AC 4 ms
8,252 KB
testcase_06 RE -
testcase_07 AC 11 ms
8,256 KB
testcase_08 AC 7 ms
8,300 KB
testcase_09 AC 8 ms
8,344 KB
testcase_10 AC 4 ms
8,232 KB
testcase_11 AC 3 ms
8,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>

using namespace std;


int main() {
	int n, total;
	bool dp[50][100001] = {};

	cin >> n;
	cin >> total;

	vector<int> a(n, 0);

	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	dp[0][a[0]] = true;
	for (int i = 1; i < n; i++) {
		for (int j = 0; j <= total; j++) {
			if (dp[i-1][j]) {
				if (j + a[i] <= total) {
					dp[i][j + a[i]] = true;
				}
				if (j * a[i] <= total) {
					dp[i][j * a[i]] = true;
				};
			}
		}
	}

	for (int i = 0; i < total; i++) {
		dp[n - 1][i] = false;
	}
	for (int i = n - 2; i >= 0; i--) {
		for (int j = 0; j <= total; j++) {
			if (dp[i][j]) {
				if (
					(dp[i][j] + a[i+1] <= total && dp[i+1][j + a[i+1]]) ||
					(dp[i][j] * a[i+1] <= total && dp[i+1][j * a[i+1]])
				) {
					// printf("ok %2d %6d\n", i, j);
					; // 処理なし
				} else {
					dp[i][j] = false;
				}
			}
		}
	}

	string ans(n - 1, '?');
	int calc = a[0];
	for (int i = 1 ; i < n; i++) {
		if (dp[i][calc + a[i]]) {
			calc += a[i];
			ans[i-1] = '+';
		} else {
			calc *= a[i];
			ans[i-1] = '*';
		}
	}

	cout << ans << endl;
	return 0;
}
0