結果

問題 No.2927 Reverse Polish Equation
ユーザー loop0919loop0919
提出日時 2024-07-18 00:38:29
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 339 ms / 2,000 ms
コード長 1,188 bytes
コンパイル時間 3,073 ms
コンパイル使用メモリ 253,248 KB
実行使用メモリ 10,420 KB
最終ジャッジ日時 2024-10-16 00:20:32
合計ジャッジ時間 9,000 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 2 ms
6,820 KB
testcase_02 AC 2 ms
6,820 KB
testcase_03 AC 2 ms
6,816 KB
testcase_04 AC 2 ms
6,820 KB
testcase_05 AC 2 ms
6,820 KB
testcase_06 AC 2 ms
6,816 KB
testcase_07 AC 3 ms
6,816 KB
testcase_08 AC 3 ms
6,816 KB
testcase_09 AC 3 ms
6,816 KB
testcase_10 AC 2 ms
6,820 KB
testcase_11 AC 4 ms
6,820 KB
testcase_12 AC 80 ms
6,820 KB
testcase_13 AC 143 ms
6,816 KB
testcase_14 AC 99 ms
6,816 KB
testcase_15 AC 122 ms
6,816 KB
testcase_16 AC 44 ms
6,820 KB
testcase_17 AC 171 ms
7,160 KB
testcase_18 AC 216 ms
7,996 KB
testcase_19 AC 15 ms
6,820 KB
testcase_20 AC 177 ms
7,124 KB
testcase_21 AC 251 ms
9,096 KB
testcase_22 AC 24 ms
6,816 KB
testcase_23 AC 2 ms
6,816 KB
testcase_24 AC 2 ms
6,820 KB
testcase_25 AC 3 ms
6,820 KB
testcase_26 AC 2 ms
6,820 KB
testcase_27 AC 243 ms
8,352 KB
testcase_28 AC 264 ms
7,520 KB
testcase_29 AC 35 ms
6,820 KB
testcase_30 AC 294 ms
8,240 KB
testcase_31 AC 16 ms
6,820 KB
testcase_32 AC 66 ms
6,816 KB
testcase_33 AC 122 ms
6,816 KB
testcase_34 AC 156 ms
6,820 KB
testcase_35 AC 2 ms
6,820 KB
testcase_36 AC 206 ms
8,240 KB
testcase_37 AC 101 ms
6,816 KB
testcase_38 AC 339 ms
8,956 KB
testcase_39 AC 137 ms
6,820 KB
testcase_40 AC 277 ms
8,112 KB
testcase_41 AC 239 ms
7,104 KB
testcase_42 AC 125 ms
10,420 KB
testcase_43 AC 177 ms
10,228 KB
testcase_44 AC 205 ms
10,392 KB
testcase_45 AC 118 ms
9,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

ll rev_polish(vector<string> &S, ll x) {
    stack<ll> A;
    
    for (string s : S) {
        if (s == "+" || s == "min" || s == "max") {
            ll a = A.top();
            A.pop();
            ll b = A.top();
            A.pop();
            
            if (s == "+") {
                A.push(a + b);
            } else if (s == "min") {
                A.push(min(a, b));
            } else {
                A.push(max(a, b));
            }
            
        } else {
            if (s == "X") {
                A.push(x);
            } else {
                A.push(stoll(s));
            }
        }
    }

    return A.top();
}

int main() {
    ll Q, Y;
    cin >> Q >> Y;

    vector<string> S(Q);
    for (int i = 0; i < Q; i++) {
        cin >> S[i];
    }
    
    ll ok = Y;
    ll ng = -1;

    while (ok - ng > 1) {
        ll mid = (ok + ng) / 2;

        if (rev_polish(S, mid) >= Y) {
            ok = mid;
        } else {
            ng = mid;
        }
    }

    if (rev_polish(S, ok) == Y) {
        cout << ok << endl;
    } else {
        cout << -1 << endl;
    }

    return 0;
}
0