結果
| 問題 |
No.297 カードの数式
|
| コンテスト | |
| ユーザー |
femto
|
| 提出日時 | 2016-01-28 02:10:02 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,241 bytes |
| コンパイル時間 | 616 ms |
| コンパイル使用メモリ | 67,052 KB |
| 実行使用メモリ | 13,756 KB |
| 最終ジャッジ日時 | 2024-09-21 17:45:15 |
| 合計ジャッジ時間 | 3,096 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 1 TLE * 1 -- * 21 |
ソースコード
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 50;
int n;
ll calc(string s) {
ll sum = 0;
bool plus = true;
int i = 0;
while(i < n) {
if(i != 0) {
plus = s[i] == '+';
i++;
}
int m = 0;
while(i + m < n && '0' <= s[i + m] && s[i + m] <= '9') m++;
ll a = 0;
string ss = s.substr(i, m);
for(int j = 0; j < m; j++) {
a += ss[j] - '0';
if(j != m - 1) a *= 10;
}
if(plus) {
sum += a;
}
else {
sum -= a;
}
i += m;
}
return sum;
}
bool ok(string s) {
if(s[0] == '+' || s[0] == '-') return false;
if(s[n - 1] == '+' || s[n - 1] == '-') return false;
for(int i = 0; i < n - 1; i++) {
if((s[i] == '+' || s[i] == '-') && (s[i + 1] == '+' || s[i + 1] == '-'))
return false;
}
return true;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
string s;
cin >> n;
for(int i = 0; i < n; i++) {
char c;
cin >> c;
s += c;
}
sort(s.begin(), s.end());
ll ans_max = -INF, ans_min = INF;
do {
if(ok(s)) {
ll m = calc(s);
ans_max = max(ans_max, m);
ans_min = min(ans_min, m);
}
} while(next_permutation(s.begin(), s.end()));
cout << ans_max << " " << ans_min << endl;
}
femto