結果
問題 | No.2927 Reverse Polish Equation |
ユーザー |
![]() |
提出日時 | 2025-01-24 00:28:08 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,574 bytes |
コンパイル時間 | 3,099 ms |
コンパイル使用メモリ | 137,988 KB |
実行使用メモリ | 10,280 KB |
最終ジャッジ日時 | 2025-01-24 00:28:17 |
合計ジャッジ時間 | 8,119 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 WA * 10 |
ソースコード
#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, inf) < y){cout << -1 << endl;return 0;}if(solve(v, 0) == y){cout << 0 << endl;return 0;}ll left, right; left = 0; right = inf;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;}