結果
| 問題 |
No.470 Inverse S+T Problem
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-12-19 16:49:29 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,690 bytes |
| コンパイル時間 | 1,515 ms |
| コンパイル使用メモリ | 105,676 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2024-12-22 13:08:10 |
| 合計ジャッジ時間 | 5,402 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 WA * 3 |
| other | AC * 12 WA * 14 TLE * 1 |
ソースコード
// WA
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
#define repeat_from(i,m,n) for (int i = (m); (i) < int(n); ++(i))
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
using namespace std;
int main() {
int n; cin >> n;
vector<string> s(n); repeat (i,n) cin >> s[i];
assert (1 <= n and n <= 100000);
repeat (i,n) {
assert (s[i].length() == 3);
for (char c : s[i]) assert ('A' <= c and c <= 'Z' or 'a' <= c and c <= 'z');
}
vector<bool> result;
if (n <= (26*2)*(26*2+1)) {
map<string, int> cnt;
repeat (i,n) {
cnt[s[i].substr(0, 1)] += 1;
cnt[s[i].substr(1, 2)] += 1;
cnt[s[i].substr(0, 2)] += 1;
cnt[s[i].substr(2, 1)] += 1;
}
auto score = [&](pair<int, bool> it) {
int i; bool p; tie(i, p) = it;
int a, b;
if (p) {
a = cnt[s[i].substr(0, 1)];
b = cnt[s[i].substr(1, 2)];
} else {
a = cnt[s[i].substr(0, 2)];
b = cnt[s[i].substr(2, 1)];
}
return a + b;
};
vector<pair<int, bool> > xs;
repeat (i,n) repeat (p,2) xs.emplace_back(i, p);
result.resize(n);
set<string> used;
vector<bool> decided(n);
while (not xs.empty()) {
auto it = whole(min_element, xs, [&](auto x, auto y) { return score(x) < score(y); });
int i; bool p; tie(i, p) = *it;
xs.erase(it);
if (decided[i]) continue;
string a, b;
if (p) {
a = cnt[s[i].substr(0, 1)];
b = cnt[s[i].substr(1, 2)];
} else {
a = cnt[s[i].substr(0, 2)];
b = cnt[s[i].substr(2, 1)];
}
if (not used.count(a) and not used.count(b)) {
result[i] = p;
decided[i] = true;
used.insert(a);
used.insert(b);
}
}
repeat (i,n) {
if (not decided[i]) {
result.clear();
break;
}
}
}
if (result.empty()) {
cout << "Impossible" << endl;
} else {
repeat (i,n) {
if (result[i]) {
cout << s[i][0] << ' ' << s[i][1] << s[i][2] << endl;
} else {
cout << s[i][0] << s[i][1] << ' ' << s[i][2] << endl;
}
}
}
return 0;
}