結果
| 問題 | No.2927 Reverse Polish  Equation | 
| コンテスト | |
| ユーザー |  startcpp | 
| 提出日時 | 2024-10-12 16:47:41 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 440 ms / 2,000 ms | 
| コード長 | 2,023 bytes | 
| コンパイル時間 | 1,197 ms | 
| コンパイル使用メモリ | 87,772 KB | 
| 実行使用メモリ | 11,320 KB | 
| 最終ジャッジ日時 | 2024-10-16 00:27:30 | 
| 合計ジャッジ時間 | 9,301 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 43 | 
ソースコード
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cstdio>
#include <cmath>
#include <cassert>
#define rep(i, n) for(i = 0; i < n; i++)
#define int long long
using namespace std;
typedef pair<int, int> P;
int q, Y;
string s[200000];
int calc(int X) {
    const int INF = 1e+18;
    vector<int> stk;
    for (int i = 0; i < q; i++) {
        if (s[i] == "X") {
            stk.push_back(X);
        }
        else if (s[i] == "+") {
            int a = stk[stk.size() - 2];
            int b = stk[stk.size() - 1];
            stk.pop_back();
            stk.pop_back();
            stk.push_back(min(INF, a + b));
        }
        else if (s[i] == "min") {
            int a = stk[stk.size() - 2];
            int b = stk[stk.size() - 1];
            stk.pop_back();
            stk.pop_back();
            stk.push_back(min(a, b));
        }
        else if (s[i] == "max") {
            int a = stk[stk.size() - 2];
            int b = stk[stk.size() - 1];
            stk.pop_back();
            stk.pop_back();
            stk.push_back(max(a, b));
        }
        else {
            int val = 0;
            for (int j = 0; j < s[i].length(); j++) {
                val *= 10;
                val += s[i][j] - '0';
            }
            stk.push_back(val);
        }
        /*cout << "i: " << i << ", X: " << X << endl;
        for (int j = 0; j < stk.size(); j++) {
            cout << stk[j] << " ";
        }
        cout << endl;*/
    }
    return stk[0];
}
signed main() {
    int i;
    cin >> q >> Y;
    rep(i, q) cin >> s[i];
    //まじでいい問題
    int ng = -1, ok = 1e+15, mid;
    while (ok - ng >= 2) {
        mid = (ok + ng) / 2;
        int res = calc(mid);
        if (res >= Y) ok = mid;
        else ng = mid;
    }
    if (calc(ok) == Y) {
        cout << ok << endl;
    }
    else {
        cout << -1 << endl;
    }
    return 0;
}
            
            
            
        