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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sugarbliss/article/details/85056296

题目链接: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;
}

猜你喜欢

转载自blog.csdn.net/sugarbliss/article/details/85056296
今日推荐