結果

問題 No.10 +か×か
ユーザー MisterMister
提出日時 2020-08-25 20:58:02
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 1,224 bytes
コンパイル時間 949 ms
コンパイル使用メモリ 75,516 KB
最終ジャッジ日時 2025-01-13 14:42:27
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

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