結果

問題 No.2927 Reverse Polish Equation
ユーザー 👑 loop0919
提出日時 2024-06-13 04:38:09
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 316 ms / 2,000 ms
コード長 1,275 bytes
コンパイル時間 3,724 ms
コンパイル使用メモリ 281,604 KB
実行使用メモリ 10,440 KB
最終ジャッジ日時 2024-10-16 00:19:57
合計ジャッジ時間 9,631 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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(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;
        }
    }
    
    cout << (rpn(ok) == Y ? ok : -1) << endl;
    
    return 0;
}
0