codeforces1081/B(思维题)

B. Farewell Party
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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,…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,…,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.

Input

The first line contains a single integer nn (1≤n≤10^5), the number of persons in the party.

The second line contains nn integersa1,a2,…,an (0≤ai≤n−1), the statements of people.

Output

If there is no solution, print a single line "Impossible".

Otherwise, print "Possible" and then nn integers b1,b2,,bn(1≤bi≤n).

If there are multiple answers, print any of them.

Examples
input
Copy
3
0 0 0
output
Copy
Possible
1 1 1
input
Copy
5
3 3 2 2 2
output
Copy
Possible
1 1 2 2 2
input
Copy
4
0 1 2 3
output
Copy
Impossible
Note

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 1.

In the answer to the second example, the first and the second person wore the hat with type 1 and all other wore a hat of type 2.

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.

请注意

在第一个例子的答案中,所有的帽子都是一样的,所以每个人都会说没有人戴的帽子不同于kind 1。

在第二个例子的答案中,第一和第二个人戴的是1型的帽子,其他人都戴的是2型的帽子。前两个人会说有三个人戴着和他们不同的帽子。同样的,最后三个人会说有两个人戴着一顶与他们自己不同的帽子。

在第三个示例中,可以显示不存在任何解决方案。

**在第一个和第二个示例中,其他可能的配置也是可能的。

题意:有n个人,每个人编号是1~n,他们每个人都戴了一顶帽子,现在,你不知道每个人戴了什么帽子,但是他们都会给一个他们知道与自己帽子不同颜色的人数给你,你要用这n个数推出一种与所有人给的与自己不同帽子人数都符合的答案。(答案可能不止一种,输出一种就好);如果没有答案,这输出Impossible

 

思路:我的想法是首先要判断每个人说的话是否能有满足分配帽子的条件(条件在后面会推),如果都可以则给他们赋值(即戴帽子)。

 

条件:因为每个人给出的都是与自己不同帽子的人数(设为x,总人数设为n),所以就有n-x个人与他相同,那我们就要知道是否可以让说x的人的总人数(设为cnt[x])划分出可以满足n-x人数的1队或几队(注意!!!不是说同样数的人一定在一队,当时在同一队的人一定说的是同一个数,可以从样例和自己写几个例子来推导一下),因此可以得出对于每个人说的数判断是否能有满足分配帽子的条件是cnt[x]%(n-x)是否为0,若不为0,就一定不可能最终满足所有人说的条件。

至于给每个人赋值,直接看代码吧。

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 using namespace std;
 5 int cnt[100010];
 6 int vis[100010];
 7 struct node{ 
 8     int x; //输入的值 
 9     int ans; //存答案 
10     int ii;//存初始的序号 
11 }a[100010];
12 bool cmp1(node a,node b){
13     return a.ii<b.ii;
14 }
15 bool cmp2(node a,node b){
16     return a.x<b.x;
17 } 
18 int main()
19 {
20     int n;
21     cin>>n;
22     int sum=0;
23     for(int i=0;i<n;i++){
24         scanf("%d",&a[i].x);
25         a[i].ii=i;
26         cnt[a[i].x]++;
27     }
28     int flag=0;
29     for(int i=0;i<n;i++){
30         int y=n-a[i].x;
31         if(cnt[a[i].x]%y){//判断是否能分配,说相同数字的人只要判断一次即可,不过数据不大,没必要写 
32             flag=1;
33         }
34     }
35     if(flag){
36         cout<<"Impossible\n";
37     }else{
38 //**************给每个人排序**************************// 
39         sort(a,a+n,cmp2);//按输入的x排序,方便给每个人赋值 
40         int cn=0;
41         int k=1;
42         for(int i=0;i<n;i++){
43             if(cn==0){
44                 cn=n-a[i].x;//记录和他戴帽子相同的人数有几个 
45             }
46             if(cn){
47                 a[i].ans=k;//戴帽子 
48                 cn--;//每给一个人戴一次这种颜色的帽子,人数就要减一 
49                 if(cn==0) k++;//人数为0,则帽子颜色要换 
50             }
51         }
52         sort(a,a+n,cmp1);//按序号排序,恢复初始的顺序,再输出 
53         cout<<"Possible\n";
54         for(int i=0;i<n;i++){
55             cout<<a[i].ans<<" ";
56         }
57         cout<<"\n";
58     }
59     return 0;
60 }

人太菜了,错了请指正。

猜你喜欢

转载自www.cnblogs.com/liuzuolin/p/10482799.html