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
52 1 3 0 1
Output
Possible4 5 1 3 2
Input
90 2 3 4 1 1 0 2 2
Output
Possible7 5 2 1 6 8 3 4 9
Input
40 2 1 1
Output
Impossible
题意:有n个人进一个房间,如果进来的时候,里面有人,他要与每个人都握一次手,当房间里的人数大于等于三人的时候,其中有三个人可以组队离开,现在给出每个人进来时握手的次数,要求输出进来的顺序,如果方案不唯一,任意输出
思路:我们可以直接模拟暴力输出,握手次数决定了顺序
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- #include <stack>
- #include <queue>
- #include <map>
- #include <set>
- #include <vector>
- #include <math.h>
- #include <algorithm>
- using namespace std;
- #define ls 2*i
- #define rs 2*i+1
- #define up(i,x,y) for(i=x;i<=y;i++)
- #define down(i,x,y) for(i=x;i>=y;i--)
- #define mem(a,x) memset(a,x,sizeof(a))
- #define w(a) while(a)
- #define LL long long
- const double pi = acos(-1.0);
- #define Len 63
- #define mod 19999997
- const int INF = 0x3f3f3f3f;
- int n,a[300000],ans[300000],len;
- vector<int> vec[300000];
- int main()
- {
- int i,j,k;
- scanf("%d",&n);
- {
- len = 0;
- up(i,1,n)
- {
- scanf("%d",&a[i]);
- vec[a[i]].push_back(i);
- }
- int now = 0,flag = 1;
- up(i,1,n)
- {
- w(now>=0 && vec[now].size()==0)
- now-=3;
- if(now<0)
- {
- flag = 0;
- break;
- }
- ans[len++] = vec[now].back();
- vec[now++].pop_back();
- }
- if(flag)
- {
- printf("Possible\n");
- printf("%d",ans[0]);
- up(i,1,len-1)
- printf(" %d",ans[i]);
- printf("\n");
- }
- else
- printf("Impossible\n");
- }
- return 0;
- }