結果

問題 No.2927 Reverse Polish Equation
ユーザー 👑 loop0919loop0919
提出日時 2024-07-18 00:38:29
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 339 ms / 2,000 ms
コード長 1,188 bytes
コンパイル時間 3,073 ms
コンパイル使用メモリ 253,248 KB
実行使用メモリ 10,420 KB
最終ジャッジ日時 2024-10-16 00:20:32
合計ジャッジ時間 9,000 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

ll rev_polish(vector<string> &S, ll x) {
    stack<ll> A;
    
    for (string s : S) {
        if (s == "+" || s == "min" || s == "max") {
            ll a = A.top();
            A.pop();
            ll b = A.top();
            A.pop();
            
            if (s == "+") {
                A.push(a + b);
            } else if (s == "min") {
                A.push(min(a, b));
            } else {
                A.push(max(a, b));
            }
            
        } else {
            if (s == "X") {
                A.push(x);
            } else {
                A.push(stoll(s));
            }
        }
    }

    return A.top();
}

int main() {
    ll Q, Y;
    cin >> Q >> Y;

    vector<string> S(Q);
    for (int i = 0; i < Q; i++) {
        cin >> S[i];
    }
    
    ll ok = Y;
    ll ng = -1;

    while (ok - ng > 1) {
        ll mid = (ok + ng) / 2;

        if (rev_polish(S, mid) >= Y) {
            ok = mid;
        } else {
            ng = mid;
        }
    }

    if (rev_polish(S, ok) == Y) {
        cout << ok << endl;
    } else {
        cout << -1 << endl;
    }

    return 0;
}
0