結果
| 問題 |
No.10 +か×か
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-01-19 16:30:10 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 29 ms / 5,000 ms |
| コード長 | 1,146 bytes |
| コンパイル時間 | 466 ms |
| コンパイル使用メモリ | 62,836 KB |
| 実行使用メモリ | 8,320 KB |
| 最終ジャッジ日時 | 2024-12-14 15:26:30 |
| 合計ジャッジ時間 | 1,074 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 12 |
ソースコード
#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 (
(j + a[i+1] <= total && dp[i+1][j + a[i+1]]) ||
(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;
}