結果

問題 No.10 +か×か
ユーザー MisterMister
提出日時 2020-08-25 20:58:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 13 ms / 5,000 ms
コード長 1,224 bytes
コンパイル時間 791 ms
コンパイル使用メモリ 77,024 KB
実行使用メモリ 7,996 KB
最終ジャッジ日時 2023-08-21 06:31:48
合計ジャッジ時間 1,457 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 13 ms
7,996 KB
testcase_04 AC 10 ms
6,408 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 13 ms
7,980 KB
testcase_07 AC 10 ms
6,404 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 6 ms
4,892 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <cassert>

void solve() {
    int n, g;
    std::cin >> n >> g;

    std::vector<int> xs(n);
    for (auto& x : xs) std::cin >> x;

    std::vector<std::string> ss(n + 1, std::string(g + 1, '#'));
    ss[n][g] = '*';

    for (int i = n - 1; i >= 0; --i) {
        auto x = xs[i];

        for (int t = 0; t <= g; ++t) {
            {
                int nt = t + x;
                if (nt <= g && ss[i + 1][nt] != '#') {
                    ss[i][t] = '+';
                    continue;
                }
            }

            {
                int nt = t * x;
                if (nt <= g && ss[i + 1][nt] != '#') {
                    ss[i][t] = '*';
                }
            }
        }
    }

    assert(ss[0][0] == '+');

    std::string ans;
    int t = xs[0];
    for (int i = 1; i < n; ++i) {
        auto c = ss[i][t];
        ans.push_back(c);

        auto x = xs[i];
        if (c == '+') {
            t += x;
        } else {
            assert(c == '*');
            t *= x;
        }
    }

    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0