結果

問題 No.2927 Reverse Polish Equation
ユーザー Hydrogen332
提出日時 2024-10-30 12:55:35
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 182 ms / 2,000 ms
コード長 1,267 bytes
コンパイル時間 2,203 ms
コンパイル使用メモリ 202,476 KB
最終ジャッジ日時 2025-02-25 01:46:15
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)s; i < (int)e; ++i)
#define all(a) (a).begin(), (a).end()

ll culc(ll X,ll N, vector<string> &s, vector<ll> &num) {
    stack<ll> st;
    
    rep(i, 0, N) {
        if (s[i] == "X") {
            st.push(X);
            continue;
        }
        if (num[i] != -1) {
            st.push(num[i]);
            continue;
        }
        
        ll top = st.top();
        st.pop();
        
        if (s[i] == "max") st.top() = max(st.top(), top);
        else if (s[i] == "min") st.top() = min(st.top(), top);
        else st.top() = st.top() + top;
    }
    
    return st.top();
}

int main() {
    cin.tie(nullptr);
    
    ll Q, Y;
    cin >> Q >> Y;
    
    vector<string> s(Q);
    vector<ll> num(Q, -1);
    rep(i, 0, Q) {
        cin >> s[i];
        
        if (s[i] != "min" && s[i] != "max" && s[i] != "+" && s[i] != "X") {
            num[i] = stoi(s[i]);
        }
    }
    
    ll ng = -1, ok = Y;
    while (ok - ng > 1) {
        ll mid = (ok - ng) / 2 + ng;
        
        if (culc(mid, Q, s, num) >= Y) ok = mid;
        else ng = mid;
    }
    
    if (culc(ok, Q, s, num) == Y) cout << ok << '\n';
    else cout << -1 << '\n';
}
0