結果

問題 No.297 カードの数式
ユーザー asi1024asi1024
提出日時 2015-11-12 22:06:44
言語 C++11
(gcc 13.3.0)
結果
TLE  
実行時間 -
コード長 1,175 bytes
コンパイル時間 1,716 ms
コンパイル使用メモリ 170,372 KB
実行使用メモリ 13,764 KB
最終ジャッジ日時 2024-09-13 14:32:15
合計ジャッジ時間 4,067 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 1 TLE * 1 -- * 21
権限があれば一括ダウンロードができます

ソースコード

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