結果

問題 No.10 +か×か
ユーザー hanyuhanyu
提出日時 2020-12-26 17:17:54
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 687 ms / 5,000 ms
コード長 939 bytes
コンパイル時間 2,141 ms
コンパイル使用メモリ 206,956 KB
実行使用メモリ 305,324 KB
最終ジャッジ日時 2023-08-21 06:32:45
合計ジャッジ時間 4,787 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 687 ms
305,324 KB
testcase_04 AC 119 ms
107,580 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 685 ms
304,216 KB
testcase_07 AC 295 ms
150,860 KB
testcase_08 AC 67 ms
39,700 KB
testcase_09 AC 79 ms
61,820 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  
  int n, t;
  cin >> n >> t;
  
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a.at(i);
  
  vector<vector<string>> dp(n, vector<string>(t + 1, ""));
  dp[0][a[0]] += '!';
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j <= t; j++) {
      if (dp[i][j] == "") continue;
      if (j + a[i + 1] <= t) {
        string keep = dp[i][j] + '+';
        if (dp[i + 1][j + a[i + 1]] == "") dp[i + 1][j + a[i + 1]] = keep;
        else if (dp[i + 1][j + a[i + 1]] < keep) dp[i + 1][j + a[i + 1]] = keep;
      }
      if (j * a[i + 1] <= t) {
        string keep = dp[i][j] + '*';
        if (dp[i + 1][j * a[i + 1]] == "") dp[i + 1][j * a[i + 1]] = keep;
        else if (dp[i + 1][j * a[i + 1]] < keep) dp[i + 1][j * a[i + 1]] = keep;
      }
    }
  }
  
  string ans = dp[n - 1][t].substr(1);
  cout << ans << '\n';
}
0