結果

問題 No.297 カードの数式
ユーザー femtofemto
提出日時 2016-01-28 02:10:02
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,241 bytes
コンパイル時間 613 ms
コンパイル使用メモリ 66,484 KB
実行使用メモリ 8,696 KB
最終ジャッジ日時 2023-10-21 16:29:39
合計ジャッジ時間 3,415 ms
ジャッジサーバーID
(参考情報)
judge14 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,696 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 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 <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;
}
0