結果

問題 No.2927 Reverse Polish Equation
ユーザー VvyLw
提出日時 2024-10-14 17:58:33
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 343 ms / 2,000 ms
コード長 1,558 bytes
コンパイル時間 1,943 ms
コンパイル使用メモリ 128,584 KB
実行使用メモリ 11,176 KB
最終ジャッジ日時 2024-10-16 00:34:01
合計ジャッジ時間 7,504 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#ifdef local
#include <C++/core/io/debug_print.hpp>
#else
#define dump(...) void(0);
#endif

#include <iostream>
#include <ranges>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
namespace man {
typedef __int128_t i128;
}
int main() {
    std::cin.tie(nullptr) -> sync_with_stdio(false);
    using namespace std::views;
    int q;
    int64_t y;
    std::cin >> q >> y;
    std::vector<std::string> s(q);
    for(auto &t: s) {
        std::cin >> t;
    }
    const auto f = [&s](const man::i128 x) -> man::i128 {
        std::stack<man::i128> sk;
        for(const auto &t: s) {
            if(t == "+" || t == "min" || t == "max") {
                const man::i128 y = sk.top();
                sk.pop();
                const man::i128 x = sk.top();
                sk.pop();
                if(t == "+") {
                    sk.emplace(x + y);
                } else if(t == "min") {
                    sk.emplace(std::min(x, y));
                } else {
                    sk.emplace(std::max(x, y));
                }
            } else {
                sk.emplace(t == "X" ? x : std::stol(t));
            }
        }
        return sk.top();
    };
    int64_t ok = (1L << 61) - 1, ng = -1;
    while(std::abs(ok - ng) > 1) {
        const man::i128 x = ((man::i128) ok + ng) / 2;
        (f(x) >= y ? ok : ng) = x;
    }
    std::cout << (f(ok) == y ? ok : -1) << '\n';
}
0