結果

問題 No.297 カードの数式
ユーザー sugim48sugim48
提出日時 2015-11-06 23:34:02
言語 C++11
(gcc 11.4.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,515 bytes
コンパイル時間 1,445 ms
コンパイル使用メモリ 86,200 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-11 14:59:01
合計ジャッジ時間 4,343 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

using namespace std;
#include <algorithm>
#include <iostream>
#include <random>
#include <string>
#include <fstream>
#include <vector>
#include <climits>

typedef long long ll;
ll INF = LLONG_MAX / 2;

int main() {
	int N; cin >> N;
	int plus = 1, minus = 0;
	vector<int> a;
	while (N--) {
		string s; cin >> s;
		char c = s[0];
		if (c == '+') plus++;
		else if (c == '-') minus++;
		else a.push_back(c - '0');
	}
	int n = a.size();
	sort(a.begin(), a.end());
	vector<ll> ma(1<<n, -INF), mi(1<<n, INF);
	ma[0] = mi[0] = 0;
	while (plus--) {
		vector<ll> _ma(1<<n, -INF), _mi(1<<n, INF);
		for (int S = 0; S < 1<<n; S++)
			for (int T = 1; T < 1<<n; T++) {
				if (S & T) continue;
				ll x = 0;
				for (int i = 0; i < n; i++)
					if (T>>i & 1)
						x = x * 10 + a[i];
				ll y = 0;
				for (int i = n - 1; i >= 0; i--)
					if (T>>i & 1)
						y = y * 10 + a[i];
				_ma[S | T] = max(_ma[S | T], ma[S] + y);
				_mi[S | T] = min(_mi[S | T], mi[S] + x);
			}
		ma = _ma;
		mi = _mi;
	}
	while (minus--) {
		vector<ll> _ma(1<<n, -INF), _mi(1<<n, INF);
		for (int S = 0; S < 1<<n; S++)
			for (int T = 1; T < 1<<n; T++) {
				if (S & T) continue;
				ll x = 0;
				for (int i = 0; i < n; i++)
					if (T>>i & 1)
						x = x * 10 + a[i];
				ll y = 0;
				for (int i = n - 1; i >= 0; i--)
					if (T>>i & 1)
						y = y * 10 + a[i];
				_ma[S | T] = max(_ma[S | T], ma[S] - x);
				_mi[S | T] = min(_mi[S | T], mi[S] - y);
			}
		ma = _ma;
		mi = _mi;
	}
	cout << ma[(1<<n) - 1] << ' ' << mi[(1<<n) - 1] << endl;
}
0