CodeForces 534D Program B

Description

On February, 30th n students came in the Center for Training Olympiad Programmers (CTOP) of the Berland State University. They came one by one, one after another. Each of them went in, and before sitting down at his desk, greeted with those who were present in the room by shaking hands. Each of the students who came in stayed in CTOP until the end of the day and never left.

At any time any three students could join together and start participating in a team contest, which lasted until the end of the day. The team did not distract from the contest for a minute, so when another student came in and greeted those who were present, he did not shake hands with the members of the contest writing team. Each team consisted of exactly three students, and each student could not become a member of more than one team. Different teams could start writing contest at different times.

Given how many present people shook the hands of each student, get a possible order in which the students could have come to CTOP. If such an order does not exist, then print that this is impossible.

Please note that some students could work independently until the end of the day, without participating in a team contest.

Input

The first line contains integer n (1 ≤ n ≤ 2·105) — the number of students who came to CTOP. The next line contains n integers a1, a2, ..., an (0 ≤ ai < n), where ai is the number of students with who the i-th student shook hands.

Output

If the sought order of students exists, print in the first line "Possible" and in the second line print the permutation of the students' numbers defining the order in which the students entered the center. Number i that stands to the left of number j in this permutation means that the i-th student came earlier than the j-th student. If there are multiple answers, print any of them.

If the sought order of students doesn't exist, in a single line print "Impossible".

Sample Input

Input
5
2 1 3 0 1
Output
Possible
4 5 1 3 2
Input
9
0 2 3 4 1 1 0 2 2
Output
Possible
7 5 2 1 6 8 3 4 9
Input
4
0 2 1 1
Output
Impossible






 
题意:有n个人进一个房间,如果进来的时候,里面有人,他要与每个人都握一次手,当房间里的人数大于等于三人的时候,其中有三个人可以组队离开,现在给出每个人进来时握手的次数,要求输出进来的顺序,如果方案不唯一,任意输出
 
思路:我们可以直接模拟暴力输出,握手次数决定了顺序
 
 
 
 
 
  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4. #include <stack>  
  5. #include <queue>  
  6. #include <map>  
  7. #include <set>  
  8. #include <vector>  
  9. #include <math.h>  
  10. #include <algorithm>  
  11. using namespace std;  
  12. #define ls 2*i  
  13. #define rs 2*i+1  
  14. #define up(i,x,y) for(i=x;i<=y;i++)  
  15. #define down(i,x,y) for(i=x;i>=y;i--)  
  16. #define mem(a,x) memset(a,x,sizeof(a))  
  17. #define w(a) while(a)  
  18. #define LL long long  
  19. const double pi = acos(-1.0);  
  20. #define Len 63  
  21. #define mod 19999997  
  22. const int INF = 0x3f3f3f3f;  
  23.   
  24. int n,a[300000],ans[300000],len;  
  25. vector<int> vec[300000];  
  26.   
  27. int main()  
  28. {  
  29.     int i,j,k;  
  30.     scanf("%d",&n);  
  31.     {  
  32.         len = 0;  
  33.         up(i,1,n)  
  34.         {  
  35.             scanf("%d",&a[i]);  
  36.             vec[a[i]].push_back(i);  
  37.         }  
  38.         int now = 0,flag = 1;  
  39.         up(i,1,n)  
  40.         {  
  41.             w(now>=0 && vec[now].size()==0)  
  42.             now-=3;  
  43.             if(now<0)  
  44.             {  
  45.                 flag = 0;  
  46.                 break;  
  47.             }  
  48.             ans[len++] = vec[now].back();  
  49.             vec[now++].pop_back();  
  50.         }  
  51.         if(flag)  
  52.         {  
  53.             printf("Possible ");  
  54.             printf("%d",ans[0]);  
  55.             up(i,1,len-1)  
  56.             printf(" %d",ans[i]);  
  57.             printf(" ");  
  58.         }  
  59.         else  
  60.         printf("Impossible ");  
  61.     }  
  62.   
  63.     return 0;  
  64. }  
原文地址:https://www.cnblogs.com/xl1164191281/p/4678535.html