結果

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main(){
    ll Q, Y;
    cin >> Q >> Y;
    
    vector<string> S(Q);
    for (int i = 0; i < Q; i++) {
        cin >> S[i];
    }
    
    auto rpn = [&](ll x) -> ll {
        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((ll)stoi(s));
                }
            }
        }
        return A.top();
    };
    
    ll ok = (ll) 1e14;
    ll ng = -1LL;
    
    while (ok - ng > 1) {
        ll mid = (ok + ng) / 2;
        
        if (rpn(mid) >= Y) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    if (rpn(ok) == Y) {
        cout << ok << endl;
    } else {
        cout << -1 << endl;
    }
    
    return 0;
}
0