結果

問題 No.2927 Reverse Polish Equation
ユーザー karinohitokarinohito
提出日時 2024-10-14 14:30:46
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 336 ms / 2,000 ms
コード長 749 bytes
コンパイル時間 2,440 ms
コンパイル使用メモリ 208,392 KB
実行使用メモリ 10,240 KB
最終ジャッジ日時 2024-10-16 00:32:10
合計ジャッジ時間 8,544 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
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('0'<=S[i][0]&&S[i][0]<='9'){
        	P.push(stoll(S[i]));
        }
        else{
        	ll y=P.top();
            P.pop();
            ll z=P.top();
            P.pop();
            P.push(S[i]=="+"?y+z:S[i][1]=='i'?min(y,z):max(y,z));
        }
    }
    return P.top();
}

int main() {
	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;
}
0