1. 程式人生 > >Avito Cool Challenge 2018-B. Farewell Party(思維)

Avito Cool Challenge 2018-B. Farewell Party(思維)

題目連結:http://codeforces.com/contest/1081/problem/B

題意:有n個人,接下來一行n個數a[i] 表示第i個人描述和其他人有a[i]個的帽子跟他不一樣,帽子編號為1~n 如果所有的描述都是正確的輸出Possible 再輸出一行b[i] 表示第i個人的帽子的編號,如果存在矛盾 輸出Impossible。

思路:比如有n  = 5個人則有5種帽子,如果 a[i] = 3,表示有3個人和他的帽子不同,那麼n - a[i] 表示有2個人(包括他)帽子相同相同。可以得到 a[i] = 3的個數 % (n -  a[i])  != 0 就是Impossible,反之就是Possible,那麼我們存一下每一個人的編號,然後構造序列就可以了。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 120000;
int n, a[maxn];
map<int, vector<int> > mm;
int ans[maxn];
int main()
{
	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		cin >> a[i];
		a[i] = n - a[i];
		mm[a[i]].push_back(i);
	}
	int cc = 1;
	for(auto v: mm)
	{
		if(v.second.size() % v.first != 0)
		{
			cout << "Impossible\n";
			return 0;
		}
		for(int i = 0; i < v.second.size(); ++i)
			ans[v.second[i]] = cc + i / v.first;
		cc += v.second.size() / v.first;
	}
	cout << "Possible\n";
	for (int i = 0; i < n; ++i)
		cout << ans[i] << " ";
	cout << "\n";
	return 0;
}