結果
| 問題 |
No.297 カードの数式
|
| コンテスト | |
| ユーザー |
sugim48
|
| 提出日時 | 2015-11-06 23:38:12 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,547 bytes |
| コンパイル時間 | 972 ms |
| コンパイル使用メモリ | 92,352 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-09-13 13:51:34 |
| 合計ジャッジ時間 | 3,540 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 WA * 2 |
| other | AC * 10 WA * 13 |
ソースコード
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++) {
int cS = (1<<n) - 1 - S;
for (int T = cS; T; T = (T - 1) & cS) {
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++) {
int cS = (1<<n) - 1 - S;
for (int T = cS; T; T = (T - 1) & cS) {
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;
}
cout << ma[(1<<n) - 1] << ' ' << mi[(1<<n) - 1] << endl;
}
sugim48