結果

問題 No.2927 Reverse Polish Equation
ユーザー karinohito
提出日時 2024-10-14 14:05:35
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 239 ms / 2,000 ms
コード長 1,222 bytes
コンパイル時間 2,401 ms
コンパイル使用メモリ 204,472 KB
最終ジャッジ日時 2025-02-24 19:29:10
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#include<atcoder/modint>
using namespace atcoder;
using mint = modint998244353;

vector<string> S;
ll calc(ll x){
    int N=S.size();
    stack<ll> P;
    for(int i=0;i<N;i++){
        if(S[i]=="X")P.push(x);
        else if(S[i]=="+"){
            ll y=P.top();
            P.pop();
            ll z=P.top();
            P.pop();
            P.push(y+z);
        }
        else if(S[i]=="max"){
            ll y=P.top();
            P.pop();
            ll z=P.top();
            P.pop();
            P.push(max(y,z));
        }
        else if(S[i]=="min"){
            ll y=P.top();
            P.pop();
            ll z=P.top();
            P.pop();
            P.push(min(y,z));
        }
        else P.push(stoll(S[i]));
    }
    return P.top();
}

void solve() {
    ll Q,Y;
    cin>>Q>>Y;
    S.resize(Q);
    for(int i=0;i<Q;i++)cin>>S[i];
    ll L=-1,R=Y+1;
    while(R-L>1){
        ll M=(R+L)/2;
        if(calc(M)>=Y)R=M;
        else L=M;
    }
    if(calc(R)==Y)cout<<R<<endl;
    else cout<<-1<<endl;
}

int main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);


    // ll T;
    // cin>>T;
    // while(T--)
    solve();
}
0