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开发时阅读文档时随手记下的翻译,有些地方不是很准确,但是意思还是对的,毕竟我英语也不是很好,很多句子无法做到准确的字词翻译,大家可以当做参考,有错误欢迎指出,以后我会尽力翻译的更 ...
随机推荐
- video.js的使用
跨浏览器地播放视频,在网上找了一下,找到了video.js,记录一下video.js的简单用法. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2 ...
- 关于VerilogHDL生成的锁存器
总是会遇到有写文档中提到,不要生成锁存器.问题是 一: 什么叫锁存器 二 : 为什么不要生成锁存器 三 : 如何避免生成锁存器 好,现在就这三个问题,一一做出解答 一 什么叫锁存器 锁存器(Latc ...
- Week5(10月10日):国庆之后,让我们整装期待元旦吧
Part I:提问 =========================== 1.要将“Hello, Admin",从控制器传递到视图,该如何操作? (1)使用ViewData (2)使用V ...
- gradle项目与maven项目相互转化(转)
根据build.gradle和setting.gradle文件生成idea项目: gradle idea gradle这几年发展迅猛,github越来越多的项目都开始采用gradle来构建了,但是并不 ...
- 基于visual Studio2013解决C语言竞赛题之0518回文数
题目
- yii框架网址解析问题
转载请注明来自souldak,微博:@evagle 首先如果你在config/main.php里面没有配置urlManager的话,那么流程如下(摘自yii官网) 用户发出了访问 URL http:/ ...
- WebView.destroy() called while still attached 的解决的方法
能够如今webView的父组件中删除该webview,然后再Destroy parent.removeView(webView); 然后 webView.removeAllViews(); webVi ...
- Android --Vibrator--震动服务
1.取得震动服务的句柄 vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);或者vibrator = (Vibrator)getAppli ...
- 基于Qt的简单计算器
界面: UI ui由qtdesign中托控件形成. #ifndef WIDGET_H #define WIDGET_H #include <vector> #include <QWi ...
- Light OJ 1318 Strange Game 组合数+高速幂+分解因子
长度为l的用k种字符组成的字符串有k^l中 当中m个字符要不同样 那就是k^l*C(l, m)*(k-1)^m 有反复 要除以2 可是你mod n了 不能直接除 n不一定是素数 所以不能乘以逆元 所以 ...