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.
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.
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".
5 2 1 3 0 1
Possible 4 5 1 3 2
9 0 2 3 4 1 1 0 2 2
Possible 7 5 2 1 6 8 3 4 9
4 0 2 1 1
Impossible
题意:有n个人依次走入一个房间,没进去一个人这个人就和房间里所有闲着的人握一次手,三个闲着的人可能会展开激烈的辩论从而无法和刚进来的人握手,现在给每个人和其他人握了几次手,问你n个人走进房间的顺序。
分析:贪心模拟,按进入顺序构造答案,每次找能填入第i个位置的最大的数。
#include <map>
#include <utility>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int n,x,ans[200005];
typedef vector<int> vec;
map <int,vec> f[3];
int main()
{
cin.sync_with_stdio(false);
cin>>n;
for(int i = 0;i < n;i++)
{
cin>>x;
f[x % 3][x].push_back(i);
}
int now = 0;
bool flag = false;
for(int i = 0;i < n;i++)
{
auto it = f[i % 3].upper_bound(now);
if(f[i % 3].size() && it != f[i % 3].begin())
{
now = (--it)->first + 1;
auto &u = it->second;
ans[i] = u.back();
u.pop_back();
if(u.empty()) f[i % 3].erase(it);
}
else
{
flag = true;
break;
}
}
if(flag)
{
cout<<"Impossible"<<endl;
return 0;
}
else
{
cout<<"Possible"<<endl;
for(int i = 0;i < n;i++) cout<<ans[i]+1<<" ";
}
}