Codeforces Round #298 (Div. 2) D. Handshakes [贪心]
1 second
256 megabytes
standard input
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.
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.
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".
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
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 [贪心]的更多相关文章
- Codeforces Round #298 (Div. 2) D. Handshakes 构造
D. Handshakes Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/problem ...
- Codeforces Round #298 (Div. 2)--D. Handshakes
#include <stdio.h> #include <algorithm> #include <set> using namespace std; #defin ...
- 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 ...
- CodeForces Round #298 Div.2
A. Exam 果然,并没有3分钟秒掉水题的能力,=_=|| n <= 4的时候特判.n >= 5的时候将奇数和偶数分开输出即可保证相邻的两数不处在相邻的位置. #include < ...
- Codeforces Round #180 (Div. 2) B. Sail 贪心
B. Sail 题目连接: http://www.codeforces.com/contest/298/problem/B Description The polar bears are going ...
- 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 ...
- Codeforces Round #382 (Div. 2)B. Urbanization 贪心
B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...
- Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp
题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...
- Codeforces Round #192 (Div. 1) A. Purification 贪心
A. Purification Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/329/probl ...
随机推荐
- Eigen3的安装
- sybase sql anywhere 5.0 安装后sybase central中无法打开视图等的解决办法
无法打开的原因初步分析要用英文版的xp,后来在如下处发现问题,是sql anywhere的版本太旧了, 可能没有使用Unicode编码,设置一下如下选项可以解决问题.
- 光线步进——RayMarching入门
入门实现 先用RayMarching描绘一个球体,最后在进行光照计算参考:https://www.shadertoy.com/view/llt3R4 模拟摄像机射线float3 rayDirectio ...
- 安卓获取数据demo出现的问题
时间戳是long型的数据,但其他数据都是float型,但AsyncTask要求是统一数据类型.这样我就不能把时间戳放进AsyncTask里面进行处理,我就在doInBackground中获取时间戳然后 ...
- Java编程:常见问题汇总
每天在写Java程序,其实里面有一些细节大家可能没怎么注意,这不,有人总结了一个我们编程中常见的问题.虽然一般没有什么大问题,但是最好别这样做. AD: 每天在写Java程序,其实里面有一些细节大家可 ...
- 找回Settings Sync的gist id和token
方法一:如果你本地有缓存参考:https://www.cnblogs.com/zhang1028/p/9514471.html 方法二:如果你电脑重装系统了 1.找回gist id 登陆你的githu ...
- Sdoi2014 向量集
题目描述 题解: 码力太差重构之后才$A……$ 首先求向量点积最大很容易想到凸包, 设已知$(x_0,y_0)$,求$(x,y)$满足$(x,y)*(x_0,y_0)>=(x',y')*(x_0 ...
- (13) openssl ca(签署和自建CA)
用于签署证书请求.生成吊销列表CRL以及维护已颁发证书列表和这些证书状态的数据库.因为一般人无需管理crl,所以本文只介绍openssl ca关于证书管理方面的功能. 证书请求文件使用CA的私钥签署之 ...
- phpstorm的全局操作快捷键ctrl+shift+f被搜狗占用处理方法
1.找到搜狗软件,右键选择属性设置 2.如图选择系统功能快捷键 3.去掉简繁切换快捷方式,确定后即可使用phpstorm的ctrl+shift+f来进行全局查找
- python 闭包&装饰器(一)
一.闭包 1.举例 def outer(): x = 10 def inner(): # 内部函数 print(x) # 外部函数的一个变量 return inner # 调用inner()函数的方法 ...