传送门

D. Handshakes
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 test(s)
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
Note

In the first sample from the statement the order of events could be as follows:

  • student 4 comes in (a4 = 0), he has no one to greet;
  • student 5 comes in (a5 = 1), he shakes hands with student 4;
  • student 1 comes in (a1 = 2), he shakes hands with two students (students 4, 5);
  • student 3 comes in (a3 = 3), he shakes hands with three students (students 4, 5, 1);
  • students 4, 5, 3 form a team and start writing a contest;
  • student 2 comes in (a2 = 1), he shakes hands with one student (number 1).

In the second sample from the statement the order of events could be as follows:

  • student 7 comes in (a7 = 0), he has nobody to greet;
  • student 5 comes in (a5 = 1), he shakes hands with student 7;
  • student 2 comes in (a2 = 2), he shakes hands with two students (students 7, 5);
  • students 7, 5, 2 form a team and start writing a contest;
  • student 1 comes in(a1 = 0), he has no one to greet (everyone is busy with the contest);
  • student 6 comes in (a6 = 1), he shakes hands with student 1;
  • student 8 comes in (a8 = 2), he shakes hands with two students (students 1, 6);
  • student 3 comes in (a3 = 3), he shakes hands with three students (students 1, 6, 8);
  • student 4 comes in (a4 = 4), he shakes hands with four students (students 1, 6, 8, 3);
  • students 8, 3, 4 form a team and start writing a contest;
  • student 9 comes in (a9 = 2), he shakes hands with two students (students 1, 6).

In the third sample from the statement the order of events is restored unambiguously:

  • student 1 comes in (a1 = 0), he has no one to greet;
  • student 3 comes in (or student 4) (a3 = a4 = 1), he shakes hands with student 1;
  • student 2 comes in (a2 = 2), he shakes hands with two students (students 1, 3 (or 4));
  • the remaining student 4 (or student 3), must shake one student's hand (a3 = a4 = 1) but it is impossible as there are only two scenarios: either a team formed and he doesn't greet anyone, or he greets all the three present people who work individually.

题意:

一群小盆友挨个进入教室,与教室中每个没在做contest的小盆友握手。3个小盆友可以在任意时间开始一场不会结束的contest

给出每个小盆友进教室时的握手次数。

求一个进教室的次序,满足题意。

题解:

可以发现,握手次数,增只能一个一个增,减可以幅度很大

故采取贪心策略,能增的情况就增(如果某个x在前面无法增加得到,后面就更无法达到了)

10808859                 2015-04-21 13:41:24     njczy2010     D - Handshakes             GNU C++     Accepted 233 ms 122896 KB
 #include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue> using namespace std; #define ll long long int const N = ;
int const M = ;
ll const mod = ; int n;
int a[N]; int cnt[N];
int cou[];
int ans[N];
int flag;
queue<int> que[N]; int judge()
{
if(n%==){
if(cou[]!=cou[] || cou[]!=cou[] || cou[]!=cou[]){
return ;
}
else return ;
}
else if(n%==){
if(cou[]!=cou[]+ || cou[]!=cou[]+ || cou[]!=cou[]){
return ;
}
else return ;
}
else{
if(cou[]!=cou[] || cou[]!=cou[]+ || cou[]!=cou[]+){
return ;
}
else return ;
}
return ;
} void solve()
{
int i,now;
for(i=;i<=n;i++){
while(que[i].size()>=) que[i].pop();
}
for(i=;i<=n;i++){
que[ a[i] ].push(i);
}
now=;
int te;
for(i=;i<=n;i++){
while(now>=){
if(cnt[now]>){
te=que[now].front();
que[now].pop();
ans[i]=te;
cnt[now]--;
now++;break;
}
now-=;
}
//printf(" i=%d now=%d\n",i,now);
if(now<){
flag=;break;
}
}
} void out()
{
printf("Possible\n");
int i;
printf("%d",ans[]);
for(i=;i<=n;i++){
printf(" %d",ans[i]);
}
printf("\n");
} int main()
{
//freopen("data.in","r",stdin);
//scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
while(scanf("%d",&n)!=EOF)
{
memset(cou,,sizeof(cou));
memset(cnt,,sizeof(cnt));
int i;
int j;
flag=;
for(i=;i<=n;i++){
scanf("%d",&a[i]);
j=a[i]%;
cnt[ a[i] ]++;
cou[j]++;
}
flag=judge();
// printf(" flag=%d\n",flag);
if(flag==){
printf("Impossible\n");continue;
}
solve();
if(flag==){
printf("Impossible\n");continue;
}
out();
}
return ;
}

Codeforces Round #298 (Div. 2) D. Handshakes [贪心]的更多相关文章

  1. Codeforces Round #298 (Div. 2) D. Handshakes 构造

    D. Handshakes Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/problem ...

  2. Codeforces Round #298 (Div. 2)--D. Handshakes

    #include <stdio.h> #include <algorithm> #include <set> using namespace std; #defin ...

  3. Codeforces Round #298 (Div. 2) A、B、C题

    题目链接:Codeforces Round #298 (Div. 2) A. Exam An exam for n students will take place in a long and nar ...

  4. CodeForces Round #298 Div.2

    A. Exam 果然,并没有3分钟秒掉水题的能力,=_=|| n <= 4的时候特判.n >= 5的时候将奇数和偶数分开输出即可保证相邻的两数不处在相邻的位置. #include < ...

  5. Codeforces Round #180 (Div. 2) B. Sail 贪心

    B. Sail 题目连接: http://www.codeforces.com/contest/298/problem/B Description The polar bears are going ...

  6. Codeforces Round #202 (Div. 1) A. Mafia 贪心

    A. Mafia Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/348/problem/A D ...

  7. Codeforces Round #382 (Div. 2)B. Urbanization 贪心

    B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...

  8. Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp

    题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...

  9. Codeforces Round #192 (Div. 1) A. Purification 贪心

    A. Purification Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/329/probl ...

随机推荐

  1. js的本质、全局属性

    一.js的本质 1.js的本质就是处理数据, 数据来自于后台数据库, 所以变量就起到一个临时数据的作用 Ecmascript 制定了js的数据类型 2.数据类型有哪些? 字符串(string).数字( ...

  2. Mac OS OneDrive 无法同步.DS_Store文件 出错

    Mac OS OneDrive 无法同步.DS_Store文件 同步出错 解决方案: 第一步:打开 terminal 窗口,到本地同步的目录下: cd "/Users/gkjglobal/G ...

  3. git 配置免密上传,配置ssh key

    1.windows 打开git bash 控制台,linux 直接打开命令控制台,输入 ssh-keygen 一直enter 下一步 2.生成的文件windows 存放在c://users 路径下,l ...

  4. 查看python关键字

    打开命令窗口 输入python-——help()——keywords

  5. WebGL 绘制Line的bug(三)

    之前铺垫了许多,今天可以来分享点纯干货了. 上一篇已经讲述了通过面模拟线条时候,每一个顶点的顶点数据包括:端点坐标.偏移量.前一个端点坐标.后一个端点坐标,当然如果我们通过索引的方式来绘制的话,还包括 ...

  6. nginx 配置虚拟主机访问PHP文件 502错误的解决方法

    最近配置Nginx 服务器虚拟主机 访问目录发现报502错误 百度了很多方法 都不管用  我擦 各种抓狂----- 原本Nginx配置如下: 网上找了很多方法: 查看日志   借助nginx的错误日志 ...

  7. 使用dockerfile构建nginx镜像

    使用dockerfile构建nginx镜像 docker构建镜像的方法:   commit.dockerfile 1.使用commit来构建镜像: commit是基于原有镜像基础上构建的镜像,使用此方 ...

  8. Linux test命令

    test命令 长格式的例子: test "$A" == "$B" && echo "Strings are equal" t ...

  9. 如何用纯 CSS 创作一种侧立图书的特效

    效果预览 在线演示 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. https://codepen.io/zhang-ou/pen/deVgRM 可交互视频教程 此视 ...

  10. Java:获取文件内容

    文章来源:https://www.cnblogs.com/hello-tl/p/9139353.html import java.io.*; public class FileBasicOperati ...