結果

問題 No.297 カードの数式
ユーザー asi1024asi1024
提出日時 2015-11-12 22:06:44
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,175 bytes
コンパイル時間 1,479 ms
コンパイル使用メモリ 157,108 KB
実行使用メモリ 7,564 KB
最終ジャッジ日時 2023-10-11 15:44:57
合計ジャッジ時間 4,237 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,500 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()

using namespace std;

typedef long long ll;
typedef long double ld;

const ld eps = 1e-9, pi = acos(-1.0);

int parse(vector<string> str, int &p) {
  str.push_back("@");
  int N = str.size();
  int res = 0, tmp = 0;
  string sw = "+";
  REP(i,N) {
    if (isdigit(str[i][0])) tmp = tmp * 10 + stoi(str[i]);
    else {
      if (sw == "+") res += tmp;
      else res -= tmp;
      sw = str[i];
      tmp = 0;
    }
  }
  return res;
}

bool is_valid(const vector<string> &str) {
  int N = str.size();
  if (!isdigit(str[0][0])) return false;
  if (stoi(str[0]) == 0) return false;
  if (!isdigit(str[N-1][0])) return false;
  REP(i,N-1) if (!isdigit(str[i][0]) && !isdigit(str[i+1][0])) return false;
  return true;
}

int main() {
  int N; cin >> N;
  vector<string> str(N);
  REP(i,N) cin >> str[i];
  sort(ALL(str));
  int mi = 1e9, ma = -1e9;
  do {
    int p = 0;
    if (!is_valid(str)) continue;
    int num = parse(str, p);
    mi = min(mi, num);
    ma = max(ma, num);
  } while (next_permutation(ALL(str)));
  cout << ma << " " << mi << endl;
  return 0;
}
0