結果

問題 No.470 Inverse S+T Problem
ユーザー hirokazu1020hirokazu1020
提出日時 2016-12-21 22:00:21
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 553 ms / 2,000 ms
コード長 1,814 bytes
コンパイル時間 1,359 ms
コンパイル使用メモリ 122,172 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-22 13:35:35
合計ジャッジ時間 5,761 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#include<sstream>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<numeric>
#include<functional>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_set>
#include<random>
using namespace std;
#define INF (1<<29)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define uniq(v) v.erase(unique(all(v)),v.end())



int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	int n;
	cin >> n;
	if (n > 52) {
		cout << "Impossible" << endl;
		return 0;
	}
	string u[52];
	rep(i, n)cin >> u[i];
	map<string, set<int>> mp;
	bool b[52] = {};
	rep(i, n) {
		mp[u[i].substr(0, 1)].insert(i);
		mp[u[i].substr(1, 2)].insert(i);
	}
	std::random_device rnd;
	std::mt19937 mt(rnd());
	vector<int> overlap;
	rep(i, 114514) {
		overlap.clear();
		for (auto p : mp) {
			if (p.second.size() > 1) {
				for (int a : p.second)overlap.push_back(a);
			}
		}
		if (overlap.empty())break;

		sort(all(overlap));
		uniq(overlap);
		int idx = overlap[mt() % overlap.size()];
		if (b[idx]) {
			mp[u[idx].substr(0, 2)].erase(idx);
			mp[u[idx].substr(2, 1)].erase(idx);
			mp[u[idx].substr(0, 1)].insert(idx);
			mp[u[idx].substr(1, 2)].insert(idx);
		}
		else {
			mp[u[idx].substr(0, 1)].erase(idx);
			mp[u[idx].substr(1, 2)].erase(idx);
			mp[u[idx].substr(0, 2)].insert(idx);
			mp[u[idx].substr(2, 1)].insert(idx);
		}
		b[idx] = !b[idx];
	}
	if (overlap.empty()) {
		rep(i,n) {
			if (b[i]) {
				cout << u[i].substr(0, 2) << ' ' << u[i].substr(2, 1) << endl;
			}
			else {
				cout << u[i].substr(0, 1) << ' ' << u[i].substr(1, 2) << endl;
			}
		}
	}
	else {
		cout << "Impossible" << endl;
	}
		
	return 0;
}
0