Codeforces Round #298 (Div. 2) D. Handshakes 二分
来源:互联网 发布:人工智能市场占有率 编辑:程序博客网 时间:2024/04/29 07:18
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.
The first line contains integer n (1 ≤ n ≤ 2·105) — the number of students who came to CTOP. The next line contains nintegers a1, a2, ..., an (0 ≤ ai < n), where ai is the number of students with who the i-th student shook hands.
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".
52 1 3 0 1
Possible4 5 1 3 2
90 2 3 4 1 1 0 2 2
Possible7 5 2 1 6 8 3 4 9
40 2 1 1
Impossible
#include<stdio.h>#include<string.h>#include<stack>#include<string>#include<math.h>#include<queue>#include<algorithm>#include<vector>using namespace std;#define LL __int64#define inf 1999999999#define N 200050#define min(a,b)(a<b?a:b)#define max(a,b)(a>b?a:b)struct node{int num,pos,vis;}a[N];int n,m;int cmp1(node x,node y){return x.num<y.num;}int ans[N];int bsearch(int x){int l=1,r=n,flag=-1;while(l<=r){int mid=(r+l)/2;if(x>a[mid].num)l=mid+1;else if(x<a[mid].num)r=mid-1;if(x==a[mid].num){if(a[mid].vis==0){flag=mid;r=mid-1;}else l=mid+1;}}if(flag!=-1){a[flag].vis=1;return a[flag].pos;}return -1;}int main(){int i,j;scanf("%d",&n);for(i=1;i<=n;i++){scanf("%d",&a[i].num);a[i].pos=i;a[i].vis=0;}sort(a+1,a+1+n,cmp1);int x=0;for(i=1;i<=n;i++){int tmp=bsearch(x);if(tmp==-1){while(x>=3){x-=3;tmp=bsearch(x);if(tmp>0) break;}}if(tmp==-1) break;ans[i]=tmp;x++;}if(i<=n) printf("Impossible\n");else {printf("Possible\n");for(i=1;i<=n;i++)printf("%d ",ans[i]);printf("\n");}return 0;}
- Codeforces Round #298 (Div. 2) D. Handshakes 二分
- Codeforces Round #298 (Div. 2) D. Handshakes
- Codeforces Round #298 (Div. 2) D. Handshakes(贪心)
- Codeforces Round #298 (Div. 2) D. Handshakes(贪心)
- 534D.Handshakes(Codeforces Round #298(div2))
- #298 (div.2) D. Handshakes
- codeforces #298 D. Handshakes
- Codeforces Round #251 (Div. 2) D 二分
- Codeforces Round #352 (Div. 2) D 二分
- Codeforces Round #377 (Div. 2) D 二分
- Codeforces Round #256 (Div. 2) D. Multiplication Table 二分
- Codeforces Round #274 (Div. 2) D. Long Jumps(二分)
- Codeforces Round #220 (Div. 2) D 树状数组 && 二分
- Codeforces Round #316 (Div. 2) D. Tree Requests dfs_clock,二分
- Codeforces Round #256 (Div. 2) D Multiplication Table (二分)
- Codeforces Round #345 (Div. 2) D. Image Preview【二分】
- Codeforces Round #316 (Div. 2) D 【dfs序+二分】
- Codeforces Round #352 (Div. 2) D 二分贪心两头 + 细节
- 美团网Java工程师面试题
- easysize的使用方法 (实现控件大小位置随界面变化而有相应的变化,这个变化可以自己设置)
- EL表达式--EL输出时候属性名称该使用哪个
- javaWeb web.xml 配置
- 英语总结——木桶原理
- Codeforces Round #298 (Div. 2) D. Handshakes 二分
- java单点登录系统CAS的简单使用
- AJAX性能优化方法
- 斐波那契高效算法(4种算法综合分析)
- android Handler,Looper,Message三者关系
- 【机房收费系统】上下机
- 如何让laravel的错误显示在网页上
- App中显示html网页
- java分布式框架-JDF