結果

問題 No.470 Inverse S+T Problem
ユーザー kimiyukikimiyuki
提出日時 2016-12-19 16:49:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,690 bytes
コンパイル時間 1,278 ms
コンパイル使用メモリ 105,632 KB
実行使用メモリ 6,284 KB
最終ジャッジ日時 2023-08-24 01:01:37
合計ジャッジ時間 5,004 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
4,376 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 1 ms
4,376 KB
testcase_05 WA -
testcase_06 AC 18 ms
6,284 KB
testcase_07 AC 16 ms
6,224 KB
testcase_08 AC 18 ms
6,116 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 2 ms
4,380 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 2 ms
4,376 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 4 ms
4,376 KB
testcase_29 TLE -
testcase_30 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 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;
}
0