CodeForces 534D Program B

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 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 Input

Input

52 1 3 0 1

Output

Possible4 5 1 3 2 

Input

90 2 3 4 1 1 0 2 2

Output

Possible7 5 2 1 6 8 3 4 9

Input

40 2 1 1

Output

Impossible

题意:有n个人进一个房间,如果进来的时候,里面有人,他要与每个人都握一次手,当房间里的人数大于等于三人的时候,其中有三个人可以组队离开,现在给出每个人进来时握手的次数,要求输出进来的顺序,如果方案不唯一,任意输出

思路:我们可以直接模拟暴力输出,握手次数决定了顺序

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stack>
  5. #include <queue>
  6. #include <map>
  7. #include <set>
  8. #include <vector>
  9. #include <math.h>
  10. #include <algorithm>
  11. using namespace std;
  12. #define ls 2*i
  13. #define rs 2*i+1
  14. #define up(i,x,y) for(i=x;i<=y;i++)
  15. #define down(i,x,y) for(i=x;i>=y;i--)
  16. #define mem(a,x) memset(a,x,sizeof(a))
  17. #define w(a) while(a)
  18. #define LL long long
  19. const double pi = acos(-1.0);
  20. #define Len 63
  21. #define mod 19999997
  22. const int INF = 0x3f3f3f3f;
  23. int n,a[300000],ans[300000],len;
  24. vector<int> vec[300000];
  25. int main()
  26. {
  27. int i,j,k;
  28. scanf("%d",&n);
  29. {
  30. len = 0;
  31. up(i,1,n)
  32. {
  33. scanf("%d",&a[i]);
  34. vec[a[i]].push_back(i);
  35. }
  36. int now = 0,flag = 1;
  37. up(i,1,n)
  38. {
  39. w(now>=0 && vec[now].size()==0)
  40. now-=3;
  41. if(now<0)
  42. {
  43. flag = 0;
  44. break;
  45. }
  46. ans[len++] = vec[now].back();
  47. vec[now++].pop_back();
  48. }
  49. if(flag)
  50. {
  51. printf("Possible\n");
  52. printf("%d",ans[0]);
  53. up(i,1,len-1)
  54. printf(" %d",ans[i]);
  55. printf("\n");
  56. }
  57. else
  58. printf("Impossible\n");
  59. }
  60. return 0;
  61. }
时间: 2024-11-03 21:09:00

CodeForces 534D Program B的相关文章

CodeForces 468A Program F

Description Little X used to play a card game called "24 Game", but recently he has found it too easy. So he invented a new game. Initially you have a sequence of n integers: 1, 2, ..., n. In a single step, you can pick two of them, let's denote

CodeForces 540C Program D

Description You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where y

Codeforces 534D - Handshakes(构造,暴力)

题意:总共有N个人进入教室,   每次进入的时候会和教室里面原有的人握手,并且记录握手的次数,当教室里面有大于等于三个人的时候,任意三个人可以组队去比赛(后面近来的人将无法看到他们), 现在给出人数N    给出乱序的人的握手次数,问是否可以组成一个合法的序列 题解: 一个人一个人往教室里走,  每次看看当前握手次数的询问是否还有,  如果还有 直接用当前握手次数否则将退回当前次数减3的次数继续查询,当查询合法后将序号放入数组存下.当查询到某个次数减到负数之后退出,查询数组长度是否大于N 代码:

Codeforces 534D Handshakes 构造 模拟 贪心

题意:人们依次进大厅,后进来的人会和里面所有的人都握手, 大厅里面有三个人就 其中丧二恩就可以结伴走出大厅.给你每个人进大厅时候握手的次数.让你求一个进场顺序. 解题思路:比赛的时候是用的从后往前推.比较难,发现从前往后直接模拟就行了 . 解题代码: 1 // File Name: d.cpp 2 // Author: darkdream 3 // Created Time: 2015年04月13日 星期一 01时30分17秒 4 5 #include<vector> 6 #include&l

Codeforces 534D Handshakes(贪心)

题意   房子里有n个人走进来  编号1~n  每个人走进来时房子里所有空闲的人都会和他招手  空闲的某三个人可以选择一起去打比赛  当然打比赛就变得不空闲了  给你每个人进来时和他招手的人的数量  要求输出一种可能的进房间顺序 没有可能的就输出Impossible 这题放在d就比较简单了  直接贪心就可以  把招手数量为i对应的人都保存到栈s[i]里  第一个进房间的人肯定是s[0]里的  然后让i从0开始  s[i]非空时  让s[i]的栈顶的人进入房间  然后i++  否则让s[i-1],

Codeforce 水题报告(2)

又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数(n<=200). 首先很明显可能是区间dp,我们可以记f[i][j]为从i到j的这个多边形的三角剖分数,那么f[i][j]=f[i][k]*f[j][k]*(i,j,k是否为1个合格的三角形) CodeForces 438D:The Child and Sequence 描述:给一个序列,要求支持区

Codeforces Round #174 (Div. 2)---D. Cow Program(dp, 记忆化搜索)

Farmer John has just given the cows a program to play with! The program contains two integer variables, x and y, and performs the following operations on a sequence a1,?a2,?-,?an of positive integers: Initially, x?=?1 and y?=?0. If, after any step, x

Codeforces 283B Cow Program

题意:给你a[2] - a[n], 一个初始x = 1,初始y = 0  ,执行下面步骤 1) x +=a[x] ,y += a[x] 2) x -= a[x] ,y += a[x] 3)重复1-2步骤.只要中间x <=0 || x > n  就跳出.输出y的值 问你a[1] 从 [1,n-1]分别的值为多少. 解题思路:dfs 求 dp[i][0/1]  求以加法进入 i 和以减法进入 i 能得到的值. 解题代码: 1 // File Name: 283b.cpp 2 // Author:

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store