結果

問題 No.2927 Reverse Polish Equation
ユーザー Kyo_s_sKyo_s_s
提出日時 2024-10-12 17:03:15
言語 C++23(gcc13)
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,943 bytes
コンパイル時間 3,452 ms
コンパイル使用メモリ 283,456 KB
実行使用メモリ 11,336 KB
最終ジャッジ日時 2024-10-16 00:29:25
合計ジャッジ時間 9,905 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 1 ms
5,248 KB
testcase_07 AC 3 ms
5,248 KB
testcase_08 AC 4 ms
5,248 KB
testcase_09 AC 4 ms
5,248 KB
testcase_10 AC 2 ms
5,248 KB
testcase_11 AC 5 ms
5,248 KB
testcase_12 AC 128 ms
5,248 KB
testcase_13 AC 221 ms
6,272 KB
testcase_14 AC 154 ms
5,504 KB
testcase_15 AC 211 ms
6,144 KB
testcase_16 AC 69 ms
5,248 KB
testcase_17 AC 282 ms
7,168 KB
testcase_18 AC 340 ms
8,064 KB
testcase_19 AC 21 ms
5,248 KB
testcase_20 AC 272 ms
6,912 KB
testcase_21 AC 426 ms
9,216 KB
testcase_22 AC 40 ms
5,248 KB
testcase_23 AC 2 ms
5,248 KB
testcase_24 AC 2 ms
5,248 KB
testcase_25 AC 2 ms
5,248 KB
testcase_26 AC 2 ms
5,248 KB
testcase_27 AC 362 ms
8,320 KB
testcase_28 AC 306 ms
7,552 KB
testcase_29 AC 42 ms
5,248 KB
testcase_30 AC 340 ms
8,064 KB
testcase_31 AC 20 ms
5,248 KB
testcase_32 AC 77 ms
5,248 KB
testcase_33 AC 152 ms
5,376 KB
testcase_34 AC 187 ms
6,016 KB
testcase_35 AC 2 ms
5,248 KB
testcase_36 AC 269 ms
8,192 KB
testcase_37 AC 6 ms
5,248 KB
testcase_38 AC 19 ms
8,832 KB
testcase_39 AC 203 ms
5,504 KB
testcase_40 AC 16 ms
8,192 KB
testcase_41 AC 354 ms
7,296 KB
testcase_42 WA -
testcase_43 AC 206 ms
11,336 KB
testcase_44 AC 205 ms
11,208 KB
testcase_45 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef INCLUDED_MAIN

int main() {

    LL(Q, Y);   
    VEC(string, A, Q);

    auto eval = [&](ll x) -> ll {
        vll stack;
        for (auto a : A) {
            if (a == "+") {
                ll u = stack.back(); stack.pop_back();
                ll v = stack.back(); stack.pop_back();
                stack.pb(u + v);
            } else if (a == "min") {
                ll u = stack.back(); stack.pop_back();
                ll v = stack.back(); stack.pop_back();
                stack.pb(min(u, v));
            } else if (a == "max") {
                ll u = stack.back(); stack.pop_back();
                ll v = stack.back(); stack.pop_back();
                stack.pb(max(u, v));
            } else if (a == "X") {
                stack.pb(x);
            } else {
                stack.pb(stoll(a));
            }
        }
        return stack[0];
    };

    ll ok = INF / 4, ng = -1;
    if (eval(ok) < Y) {
        OUT(-1);
        return 0;
    }

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


    for (ll diff = -20; diff <= 20; diff++) {
        ll ook = ok + diff;
        if (ook < 0) continue;
        if (eval(ook) == Y) {
            OUT(ook);
            return 0;
        }
    }

    OUT(-1);

}

#else

#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
// https://atcoder.github.io/ac-library/master/document_ja/

/* alias */
using ull = unsigned long long;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<long long>;
using vd = vector<double>;
using vs = vector<string>;
using vb = vector<bool>;
using vpii = vector<pair<int, int>>;
using vpll = vector<pair<ll, ll>>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<long long>>;
using vvd = vector<vector<double>>;
using vvs = vector<vector<string>>;
using vvb = vector<vector<bool>>;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;

/* define */
#define MOD 998244353
// #define MOD 1000000007
#define INF (1LL << 60)
#define elif else if
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define all(obj) (obj).begin(), (obj).end()
#define YESNO(bool) cout << (bool ? "YES\n" : "NO\n")
#define YesNo(bool) cout << (bool ? "Yes\n" : "No\n")
#define yesno(bool) cout << (bool ? "yes\n" : "no\n")
template<class T> bool chmax(T &a, const T &b) {if(a<b){ a=b; return 1;} return 0;}
template<class T> bool chmin(T &a, const T &b) {if(b<a){ a=b; return 1;} return 0;}
#define debug(x) cerr << #x << ":" << x << "\n";

/* REP macro */
#define reps(i, a, n) for(ll i = (a); i < (n); i++)
#define rep(i, n) reps(i, 0, (n))
#define rrep(i, n) reps(i, 1, (n + 1))
#define repd(i, n) for(ll i = (n - 1); i >= 0; i--)
#define rrepd(i, n) for(ll i = (n); i >= 1; i--)
#define fore(i, a) for(auto &i: a)

/* vector */
template<class T> T vmax(vector<T> &array){ T ret = array[0]; for(T a: array) chmax(ret, a); return ret; }
template<class T> T vmin(vector<T> &array){ T ret = array[0]; for(T a: array) chmin(ret, a); return ret; }
template<class T> T sum(vector<T> &array){ T ret = 0; for(T a:array) ret += a; return ret; }
template<class T> void list_set(vector<T> &array){ sort(all(array)); array.erase(unique(all(array)),array.end()); }
template<class T> int bisect_left(vector<T> &array, T key){ return lower_bound(all(array),key) - array.begin(); }
template<class T> int bisect_right(vector<T> &array, T key){ return upper_bound(all(array),key) - array.begin(); }

/* string */
ll string_to_ll(string n){ ll ret = 0, k = 1; while(n.length() > 0){ ret += k * (n.back() - '0'); n.pop_back(); k *= 10; } return ret; }
string ll_to_string(ll n){ string ret = ""; while(n > 0){ ret.pb((n % 10) + '0'); n /= 10; } reverse(all(ret)); return ret; }

struct popopo{ popopo(){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(15); }; } popopoppo;

// n = 1,...,N に対して、n % A < B を満たすものの数 
ll Count_of_n_mod_A_less_than_B(ll N, ll A, ll B){ return N / A * min(A, B) + min(N % A, B - 1); }

bool include(ll l, ll r, ll x) { return l <= x && x < r; }

/* IN/OUT */
int scan() { return getchar(); }
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }
template <class T> void scan(vector<T> &);
template <class T> void scan(vector<T> &a) { for(auto &i : a) scan(i); }

void IN(){}
template<class Head, class... Tail> void IN(Head& head, Tail &...tail){ scan(head); IN(tail...); }

#define INT(...) int __VA_ARGS__; IN(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__; IN(__VA_ARGS__)
#define STR(...) string __VA_ARGS__; IN(__VA_ARGS__)
#define VEC(type, name, size) vector<type> name(size); IN(name)
#define VECS(type, name, size) vector<type> name(size + 1); for(int i = 1; i <= size; i++) scan(name[i])

void OUT(){ cout << "\n"; }
template<class T> void output(T a){ cout << a; }
template<class T> void output(vector<T> v){ for(int i = 0; i < v.size(); i++) cout << v[i] << (i == v.size() - 1 ? "" : " "); }
template<class Head, class... Tail> void OUT(const Head &head, const Tail &...tail) { output(head); if(sizeof...(tail)) cout << " "; OUT(tail...); }

void FLASH(){ cout << endl; }
template<class Head, class... Tail> void FLASH(const Head  &head, const Tail &...tail) { output(head); if(sizeof...(tail)) cout << " "; FLASH(tail...); }

namespace kyo {

long long bisect(long long ok, long long ng, function<bool(long long)> is_ok) { while (abs(ok - ng) > 1) { long long mid = ok + (ng - ok) / 2; (is_ok(mid) ? ok : ng) = mid; } return ok; }

}

#define INCLUDED_MAIN
#include __FILE__ // Codeforces で壊れるらしい

#endif
0