結果

問題 No.2927 Reverse Polish Equation
ユーザー applejam
提出日時 2025-01-24 00:31:02
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 206 ms / 2,000 ms
コード長 1,574 bytes
コンパイル時間 2,942 ms
コンパイル使用メモリ 138,316 KB
実行使用メモリ 10,368 KB
最終ジャッジ日時 2025-01-24 00:31:10
合計ジャッジ時間 6,635 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using mint = modint998244353;
#define rep(i, n) for (int i = 0; i< (int)(n); i++) 
ll inf = 1e12;

ll solve(const vector<string> &v, ll x){
    stack<ll> st;
    rep(i, v.size()){
        if(v[i] == "X"){
            st.push(x);
        }else if(v[i] == "max"){
            ll a = st.top(); st.pop();
            ll b = st.top(); st.pop();
            st.push(max(a, b));
        }else if(v[i] == "min"){
            ll a = st.top(); st.pop();
                ll b = st.top(); st.pop();
                st.push(min(a, b));
        }else if(v[i] == "+"){
            ll a = st.top(); st.pop();
                ll b = st.top(); st.pop();
                st.push(a+b);
        }else{
            ll a = stoll(v[i]);
                st.push(a);
        }

    }
    return st.top();
}

int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int q; ll y; cin >> q >> y;
    vector<string> v(q); rep(i, q)cin >> v[i];
    if(solve(v, 0) > y || solve(v, y+1) < y){
        cout << -1 << endl;
        return 0;
    }
    if(solve(v, 0) == y){
        cout << 0 << endl;
        return 0;
    }
    ll left, right; left = 0; right = y+1;
    ll mid;
    while(right - left > 1){
        mid = (left+right)/2;
        if(solve(v, mid) < y){
            left = mid;
        }else{
            right = mid;
        }
    }
    cout << (solve(v, right) == y ? right : -1) << endl;


    return 0;
}
0