传送门:http://codeforces.com/contest/1081/problem/B
B. Farewell Party
Chouti and his classmates are going to the university soon. To say goodbye to each other, the class has planned a big farewell party in which classmates, teachers and parents sang and danced.
Chouti remembered that nn persons took part in that party. To make the party funnier, each person wore one hat among nn kinds of weird hats numbered 1,2,…n1,2,…n. It is possible that several persons wore hats of the same kind. Some kinds of hats can remain unclaimed by anyone.
After the party, the ii-th person said that there were aiai persons wearing a hat differing from his own.
It has been some days, so Chouti forgot all about others' hats, but he is curious about that. Let bibi be the number of hat type the ii-th person was wearing, Chouti wants you to find any possible b1,b2,…,bnb1,b2,…,bn that doesn't contradict with any person's statement. Because some persons might have a poor memory, there could be no solution at all.
The first line contains a single integer nn (1≤n≤1051≤n≤105), the number of persons in the party.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤n−10≤ai≤n−1), the statements of people.
If there is no solution, print a single line "Impossible".
Otherwise, print "Possible" and then nn integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤n1≤bi≤n).
If there are multiple answers, print any of them.
3
0 0 0
Possible
1 1 1
5
3 3 2 2 2
Possible
1 1 2 2 2
4
0 1 2 3
Impossible
In the answer to the first example, all hats are the same, so every person will say that there were no persons wearing a hat different from kind 11.
In the answer to the second example, the first and the second person wore the hat with type 11 and all other wore a hat of type 22.
So the first two persons will say there were three persons with hats differing from their own. Similarly, three last persons will say there were two persons wearing a hat different from their own.
In the third example, it can be shown that no solution exists.
In the first and the second example, other possible configurations are possible.
题意概括:
有 N 个人,每个人都佩戴一顶帽子(帽子种类有 1、2、3 ... N );
接下来 N 个数表示所有人里面 与 第 i 个人佩戴了不同帽子的总数。
解题思路:
ai 代表与自己佩戴了不同帽子的个数,那么反过来意思就是说有 N - ai个人佩戴了与自己相同帽子。
如果能满足 数量为 ai 的 个数 Si == N - ai, 则说明刚好有 N-ai 个人佩戴相同帽子。
如果 Si > N-ai ,则需要判断 这 Si 个人里面能否内部平衡掉, 也就是分成若干块 N-ai,每一块佩戴不同的帽子,但是块内的人佩戴的帽子是相同的,这样也满足条件。
即 Si%(N-ai) ?= 0;
如果 Si % (N-ai) != 0 则说明无法平衡。
最后按块编号,输出答案。
tip:代码实现和细节很重要。
AC code:
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <cstring> 5 #include <vector> 6 #include <cmath> 7 #define INF 0x3f3f3f3f 8 using namespace std; 9 10 const int MAXN = 1e5+10; 11 vector<vector<int> >num(MAXN); 12 int ans[MAXN]; 13 14 int main() 15 { 16 int N, x; 17 scanf("%d", &N); 18 for(int i = 1; i <= N; i++){ 19 scanf("%d", &x); 20 num[N-x].push_back(i); 21 } 22 23 int tp, no = 0; 24 bool flag = true; 25 for(int i = 1; i <= N; i++){ 26 tp = num[i].size(); 27 if(tp%i != 0){ 28 flag = false; 29 break; 30 } 31 for(int j = 0; j < tp; j++){ 32 if(j%i == 0) ++no; 33 ans[num[i][j]] = no; 34 } 35 } 36 37 if(flag){ 38 puts("Possible"); 39 for(int i = 1; i <= N; i++) 40 printf("%d ", ans[i]); 41 } 42 else{ 43 puts("Impossible"); 44 } 45 return 0; 46 }