shakes hands
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 integersa1, 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
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
Hint
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.
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + ;
int q[N], n;
stack<int> s[N];
int main()
{
int a;
scanf("%d", &n);
for(int i = ; i <= n; ++i)
scanf("%d", &a), s[a].push(i); int p = , i = ;
while()
{
if(!s[i].empty())q[p++] = s[i++].top();
else
{
if(i < ) break;
s[--i].pop(), s[--i].pop(), s[--i].pop();
}
}
if(p == n)
{
puts("Possible");
for(int i = ; i < n - ; ++i)
printf("%d ", q[i]);
printf("%d\n", q[n - ]);
}
else puts("Impossible");
return ;
}
shakes hands的更多相关文章
- 每日英语:Skull Shakes Up Human Family Tree
A newly discovered 1.8 million-year-old skull offers evidence that humanity's early ancestors emerge ...
- 关于如何通过定义自己的CameraManager来控制视角
2016.8.30 发现了这个函数,可以直接获得摄像机的位置和旋转. Controller->GetPlayerViewPoint(CamLoc, CamRot); 最近看了几天PlayerCa ...
- How To Handle a Loss of Confidence in Yourself
Do you feel like you've lost confidence in yourself? Have you had strong self doubts? Perhaps you we ...
- Web开发中20个很有用的CSS库
来源: 微信公众号文章 在过去的几年中,CSS已经成为一大部分开发者和设计者的最爱,因为它提供了一系列功能和特性.每个月都有无数个围绕CSS的工具被开发者发布以简化WEB开发.像CSS库,框架,应用这 ...
- resignFirstResponder
What is this resignFirstResponder business? Here is the short version:Some view objects are also con ...
- Web 开发中 20 个很有用的 CSS 库
转自:http://www.oschina.net/translate/css-libraries-for-developers 在过去的几年中,CSS已经成为一大部分开发者和设计者的最爱,因为它提供 ...
- 越狱Season 1- Episode 18: Bluff
Season 1, Episode 18: Bluff -Michael: Scofield Scofield Michael Scofield Michael Scofield -Patoshik: ...
- TEdit,TMemo背景透明
The component below works perfectly, except for the following problem: 1) Saves the component below ...
- iOS Developer Libray (中文版)-- Defining Classes 定义类
该篇是我自己学习iOS开发时阅读文档时随手记下的翻译,有些地方不是很准确,但是意思还是对的,毕竟我英语也不是很好,很多句子无法做到准确的字词翻译,大家可以当做参考,有错误欢迎指出,以后我会尽力翻译的更 ...
随机推荐
- c#语法与c++ 及 java语法的对比分析
早期开发的时候一直用c/c++,后来主要用的是java.最近需要用下c#. 熟悉了下c#,发现c#语言在对c/c++基础上做了很多简化,同时参考了很多java的语法习惯,本来在语法上c/c++就有很多 ...
- CSipSimple最新版本号(二)--加入视频功能
前面我们编译好了最新版本号的CSipSimple,并且測试已经能够打电话了.如今要把视频功能加上去. 不知道怎么编译的,能够看我的上一篇博文:CSipSimple最新版本号 我们先来看一下之前的项目是 ...
- GUI动态创建button
#include "cocos2d.h" #include "cocos-ext.h" USING_NS_CC; USING_NS_CC_EXT; using ...
- 【Linux驱动器】Linux-2.6.20.4内核移植
最近一段时间以来一直学习TQ2440内核开发板移植.嫁接驱动器. 真诚地相信这方面的知识有很大的困难,.但有一种观点认为,从看,难度越大,的提升空间的能力更大! ! 1.解压源代码 从Internet ...
- C#后台代码编写图片地址Properties.Resources._1;
if (i == 0) { pictureBox1.Image = Properties.Resources._1; ...
- 回归基础从新认识——HTML+CSS
前言 这段时间工作没那么繁杂,索性就想说来套系统的学习,之前去面试的时候,有被问及些基础的知识,居然回答不上来,也不能说是回答不上吧,回答的不全面.前端群上问了那个机构比较好,选择了慕课网.看了一段时 ...
- 2个NASM开发环境下载以及30篇教程(lostspeed)
http://download.csdn.net/detail/lostspeed/8958175http://download.csdn.net/detail/lostspeed/8954263 h ...
- ie6下常见的bug 调整页面兼容性
ie6下常见的bug 我们布局页面,首先符合标准,如何写一个页面的标准性? 但是ie6等浏览器本身就比较特殊,bug比较多,兵法云,知己知彼百战百胜.我们需要了解ie6的一些常见bug,这样,更好的调 ...
- iTextSharp - 建立PDF文件
原文 iTextSharp - 建立PDF文件 01 using iTextSharp.text; 02 using iTextSharp.text.pdf; 03 ... 04 private vo ...
- UVALive 6931 Can't stop playing (Regionals 2014 >> Europe - Central)
题目 一开始有一个双头队列,每次添加一个数(这是数是二的幂,所有数的和不大于\(2^13\)),由你来决定添加到队头还是队尾.如果队列里面相邻的两个数相同,设它们都是\(x\),那么这两个数会合并为\ ...