結果

問題 No.2927 Reverse Polish Equation
ユーザー Polaris2124
提出日時 2025-04-25 16:06:35
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,166 ms / 2,000 ms
コード長 2,483 bytes
コンパイル時間 3,976 ms
コンパイル使用メモリ 287,636 KB
実行使用メモリ 13,932 KB
最終ジャッジ日時 2025-04-25 16:06:59
合計ジャッジ時間 23,159 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;

#define overload4(_1, _2, _3, _4, name, ...) name // 1~4個目を無視
#define rep1(n) for(ll i = 0; i < (ll)(n); ++i)
#define rep2(i, n) for(ll i = 0; i < (ll)(n); ++i)
#define rep3(i, a, b) for(ll i = (a); i < (ll)(b); ++i)
#define rep4(i, a, b, c) for(ll i = (a); i < (ll)(b); i += (ll)(c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)

#define all(x) x.begin(), x.end()

using ll = long long;
using pll = pair<ll, ll>;
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;

const int INF = 1 << 29;  const ll LINF = 0x1fffffffffffffff;

template<class T, size_t n, size_t idx = 0>
auto make_vec(const size_t (&d)[n], const T& init) noexcept {
    if constexpr (idx < n) return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));
    else return init;
}
template<class T, size_t n>
auto make_vec(const size_t (&d)[n]) noexcept {
    return make_vec(d, T{});
}

struct Edge{ Edge(ll t, ll c): to(t), cost(c) {}; ll to; ll cost; };


//======================================================================================================

ll Q, Y;
vector<string> s;

ll func(ll y){
    stack<string> st;
    rep(i, Q){
        if ('0' <= s[i][0] && s[i][0] <= '9'){
            st.push(s[i]);
        }
        else if (s[i] == "X"){
            st.push(to_string(y));
        }
        else if (s[i] == "+"){
            auto l = st.top(); st.pop();
            auto r = st.top(); st.pop();
            ll v = stoll(l) + stoll(r);
            st.push(to_string(v));
        }
        else if (s[i] == "max"){
            auto l = st.top(); st.pop();
            auto r = st.top(); st.pop();
            ll v = max(stoll(l), stoll(r));
            st.push(to_string(v));
        }
        else if (s[i] == "min"){
            auto l = st.top(); st.pop();
            auto r = st.top(); st.pop();
            ll v = min(stoll(l), stoll(r));
            st.push(to_string(v));
        }
    }

    ll val = stoll(st.top());
    return val;
}

int main(){
    cin >> Q >> Y;
    rep(i, Q) {
        string ss;
        cin >> ss;
        s.push_back(ss);
    }

    ll ok = 10000000000010;
    ll ng = -1;
    while (ok - ng > 1){
        ll mid = (ok + ng) / 2;
        if (func(mid) >= Y) ok = mid;
        else ng = mid;
    }

    if (func(ok) != Y) {
        cout << -1 << endl;
    } else{
        cout << ok << endl;
    }
}
0