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 ...
随机推荐
- jQuery选择器之子元素选择器
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...
- pre-network android 网络优化预加载框架
网络优化是所有app开发中非常重要的一部分,如果将网络请求前置就是在点击跳转activity之前开始网络加载那么速度将会有质的提升.也就是网络预先加载框框架. 网络预加载框架,监听式网络前置加载框架- ...
- Android自定义zxing扫描界面的实现
首先,我们需要导入zxing的jar文件,其次复制所需要的资源文件以及放入自己需要添加的资源文件,复制出源码的必要相关类文件.对布局文件进行一定的修改,再修改CaptureActivity与Viewf ...
- QTabelwidget 添加复选框
QString sceneName = QString("%1(%2)").arg(sisList[i].sceneName).arg(sisList[i].sceneNo); Q ...
- CAD交互绘制带颜色宽度的直线(网页版)
用户可以在CAD控件视区任意位置绘制直线. 主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下: 参数 说明 DOUBLE dX1 直线的开始点x坐标 DOUBLE ...
- 数据库_11_1~10总结回顾+奇怪的NULL
校对集问题: 比较规则:_bin,_cs,_ci利用排序(order by) 另外两种登录方式: 奇怪的NULL: NULL的特殊性:
- C-基础:数组名与取地址符&
指出下面代码的输出,并解释为什么.(不错,对地址掌握的深入挖潜) main() { ]={,,,,}; ); printf(),*(ptr-)); } 输出:2,5 *(a+1)就是a[1], ...
- 区间DP || HDU 6249 Alice’s Stamps
题意:标号为1-n的n种邮票,m个邮票集,每个集里有标号从Li到Ri的邮票,要从中选K个邮票集,使这K个邮票集能覆盖最多种的邮票,问最多能覆盖多少种邮票 思路:区间DP (我:??? f[i][j]表 ...
- 抓取oracle数据库耗费资源的sql语句
oracle数据库连接业务系统,而有些sql语句的执行严重影响了oracle的性能,就如同mysql的慢查询一样,mysql可以开启慢查询日志定位这些造成数据库性能下降的语句,而oracle同样可以做 ...
- Yii1 获取当前请求的url
echo Yii::app()->getRequest()->getUrl();