結果

問題 No.2927 Reverse Polish Equation
ユーザー Andrew8128Andrew8128
提出日時 2024-10-12 15:42:16
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 3,422 bytes
コンパイル時間 3,357 ms
コンパイル使用メモリ 258,440 KB
実行使用メモリ 13,140 KB
最終ジャッジ日時 2024-10-16 00:23:44
合計ジャッジ時間 7,985 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,820 KB
testcase_03 AC 2 ms
6,816 KB
testcase_04 AC 2 ms
6,816 KB
testcase_05 AC 2 ms
6,820 KB
testcase_06 AC 2 ms
6,820 KB
testcase_07 AC 3 ms
6,816 KB
testcase_08 AC 4 ms
6,820 KB
testcase_09 AC 3 ms
6,820 KB
testcase_10 AC 2 ms
6,816 KB
testcase_11 AC 3 ms
6,816 KB
testcase_12 AC 72 ms
6,820 KB
testcase_13 AC 122 ms
6,820 KB
testcase_14 AC 85 ms
6,816 KB
testcase_15 AC 117 ms
6,816 KB
testcase_16 AC 38 ms
6,816 KB
testcase_17 AC 151 ms
7,048 KB
testcase_18 AC 190 ms
8,036 KB
testcase_19 AC 14 ms
6,820 KB
testcase_20 AC 152 ms
7,040 KB
testcase_21 AC 232 ms
9,060 KB
testcase_22 AC 23 ms
6,824 KB
testcase_23 AC 2 ms
6,816 KB
testcase_24 AC 2 ms
6,820 KB
testcase_25 AC 2 ms
6,816 KB
testcase_26 AC 2 ms
6,820 KB
testcase_27 AC 210 ms
8,228 KB
testcase_28 AC 169 ms
7,528 KB
testcase_29 AC 24 ms
6,816 KB
testcase_30 AC 193 ms
8,096 KB
testcase_31 AC 15 ms
6,816 KB
testcase_32 AC 44 ms
6,820 KB
testcase_33 AC 83 ms
6,820 KB
testcase_34 AC 104 ms
6,820 KB
testcase_35 AC 2 ms
6,816 KB
testcase_36 AC 194 ms
8,232 KB
testcase_37 AC 6 ms
6,816 KB
testcase_38 AC 21 ms
8,916 KB
testcase_39 AC 94 ms
6,820 KB
testcase_40 AC 16 ms
7,964 KB
testcase_41 AC 154 ms
7,096 KB
testcase_42 AC 161 ms
13,140 KB
testcase_43 AC 184 ms
13,012 KB
testcase_44 AC 207 ms
13,140 KB
testcase_45 AC 153 ms
12,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, x, y) for (auto i = (x); i < (y); i++)
#define RREP(i, x, y) for (auto i = (y) - 1; (x) <= i; i--)
#define ALL(x) (x).begin(), (x).end()
#pragma GCC optimize("O3")
#define lll __int128
string to_string(lll value){
  lll tmp = value < 0 ? -value : value;
  string ret;
  do {
    ret += (tmp % 10)+'0';
    tmp /= 10;
  } while (tmp != 0);
  if(value < 0){
    ret += '-';
  }
  reverse(ret.begin(),ret.end());
  return ret;
}
ostream &operator<<(ostream &dest, lll value) {
  dest << to_string(value);
  return dest;
}
lll stolll(string &s) {
  if(s=="")return 0;
  lll ret = 0;
  for (int i = 0; i < ssize(s); i++)
    if ('0' <= s[i] && s[i] <= '9')
      ret = 10 * ret + s[i] - '0';
  if(s[0]=='-') return -ret;
  return ret;
}
istream &operator>>(istream &is, __int128_t &value){
  string s;
  is >> s;
  value = stolll(s);
  return is;
}
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  lll Q, Y;
  cin >> Q >> Y;
  vector<string> S(Q);
  REP(i, 0, Q){
    cin >> S[i];
  }
  {
    vector<lll> A;
    REP(i, 0, Q){
      if(S[i] == "X"){
        A.push_back(110000000000000ll);
      }else if(S[i] == "+"){
        lll tmp = A[ssize(A)-1] + A[ssize(A)-2];
        A.pop_back();
        A.pop_back();
        A.push_back(tmp);
      }else if(S[i] == "min"){
        lll tmp = min(A[ssize(A)-1], A[ssize(A)-2]);
        A.pop_back();
        A.pop_back();
        A.push_back(tmp);
      }else if(S[i] == "max"){
        lll tmp = max(A[ssize(A)-1], A[ssize(A)-2]);
        A.pop_back();
        A.pop_back();
        A.push_back(tmp);
      }else{
        A.push_back(stolll(S[i]));
      }
    }
    if(A[0] < Y){
      cout << -1 << "\n";
      return 0;
    }
  }
  lll l = -1, r = 110000000000000ll;
  while(1){
    if(l + 1 == r)break;
    lll c = (l + r) / 2;
    vector<lll> A;
    REP(i, 0, Q){
      if(S[i] == "X"){
        A.push_back(c);
      }else if(S[i] == "+"){
        lll tmp = A[ssize(A)-1] + A[ssize(A)-2];
        A.pop_back();
        A.pop_back();
        A.push_back(tmp);
      }else if(S[i] == "min"){
        lll tmp = min(A[ssize(A)-1], A[ssize(A)-2]);
        A.pop_back();
        A.pop_back();
        A.push_back(tmp);
      }else if(S[i] == "max"){
        lll tmp = max(A[ssize(A)-1], A[ssize(A)-2]);
        A.pop_back();
        A.pop_back();
        A.push_back(tmp);
      }else{
        A.push_back(stolll(S[i]));
      }
    }
    // cerr << c << " " << A[0] << "\n";
    if(Y <= A[0]){
      r = c;
    }else{
      l = c;
    }
    // cerr << "l " << l << " r " << r << "\n";
  }
  {
    vector<lll> A;
    REP(i, 0, Q){
      if(S[i] == "X"){
        A.push_back(r);
      }else if(S[i] == "+"){
        lll tmp = A[ssize(A)-1] + A[ssize(A)-2];
        A.pop_back();
        A.pop_back();
        A.push_back(tmp);
      }else if(S[i] == "min"){
        lll tmp = min(A[ssize(A)-1], A[ssize(A)-2]);
        A.pop_back();
        A.pop_back();
        A.push_back(tmp);
      }else if(S[i] == "max"){
        lll tmp = max(A[ssize(A)-1], A[ssize(A)-2]);
        A.pop_back();
        A.pop_back();
        A.push_back(tmp);
      }else{
        A.push_back(stolll(S[i]));
      }
    }
    if(A[0] == Y){
      cout << r << "\n";
    }else{
      cout << -1 << "\n";
    }
  }
  return 0;
}
/*
  File : ~/yukicoder/midoriika_3rd/I.cpp
  Date : 2024/10/12
  Time : 15:15:17
*/
0