传送门

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. 【转】java编程思想第20章的注解例子用到的com.sun.mirror的jar包

    Java編程思想>中的注解代码中引入过这么一个包(com.sun.mirror),书上说的是在Jdk中有个tools.jar中,引入这个包就每这个问题了,但是笔者用的是JDK 1.8,把这个包i ...

  2. nodejs中相互引用(循环引用)的模块分析

    话不多少,直接上源码吧: modA.js: module.exports.test = 'A'; const modB = require('./05_modB'); console.log( 'mo ...

  3. 在idea启动tomcat出现The JAVA_HOME environment variable is not defined correctly的解决

    情况:某套代码是用jdk 1.6编译,然后电脑的JAVA_HOME系统变量配的是jdk1.7的,在tomcat启动时报错 The JAVA_HOME environment variable is n ...

  4. Mac Outlook 15 连接office365 一直跳出登录框的问题

    解决: 比如 原登录用户名密码为 abc@xxx.com 密码:***** 改一下outlook帐号中的登录用户名为:xxx.com\abc   密码为原来密码,尝试连接,连接状态会连上,但过会儿又提 ...

  5. js数组引用

    总结归纳: 1.普通的赋值是复制栈区内容. 2.基本类型的数据在栈区存放数据自身,var a=b; //a与b无关. 引用类型数据在栈区存放数据地址.  var a=b; //a,b联动 3.基本数据 ...

  6. CSData

    NSString 转换成NSData 对象 NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncodin ...

  7. 基于 python 的接口测试框架

    项目背景 公司内部的软件采用B/S架构,管理实验室数据,实现数据的存储和分析统计.大部分是数据的增删改查,由于还在开发阶段,所以UI界面的变化非常快,之前尝试过用python+selenium进行UI ...

  8. Intel手册 Chapter23 VMX的简单介绍

    23.2  虚拟机架构 1: VMX为处理器上的虚拟机定义了处理器级的支持.VMX主要支持两类,VMM和VM 2: VMM作为HOST可以完全控制处理器和其他平台硬件. 每个VM都支持一个栈,并且由O ...

  9. 推荐一个高大上的网易云音乐命令行播放工具:musicbox

    网易云音乐上有很多适合程序猿的歌单,但是今天文章介绍的不是这些适合程序员工作时听的歌,而是一个用Python开发的开源播放器,专门适用于网易云音乐的播放.这个播放器的名称为MusicBox, 特色是用 ...

  10. APP设计细节总结-摘录

    视觉表现型问题 1. 统一的图标设计风格 2. 图标大小的视觉平衡(根据图标的体量对其大小做出相应的调整) 3. 优化你的分割线(通常我们会选择浅色而否定深色) 4. 合理的运用投影的颜色与透明度 5 ...