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 ...
随机推荐
- The Performance Manifesto
Manifesto For Performance Testing And Engineering We choose to support others in their quest for bet ...
- UVA 1175 Ladies' Choice 女士的选择(稳定婚姻问题,GS算法)
题意: 给出每个男的心目中的女神排序,给出每个女的心目中的男神排序,即两个n*n的矩阵,一旦任意两个非舞伴的男女同学觉得对方都比现任舞伴要好,他们就会抛弃舞伴而在一起.为了杜绝这种现象,求每个男的最后 ...
- About App Sandbox
沙盒是在受限的安全环境中运行应用程序的一种做法,这种做法是要限制授予应用程序的代码访问权限. 沙盒技术提供对资源的严格控制,沙盒通过限制对内存.系统文件和设置的访问,沙盒可以让企业可通过执行潜在恶意代 ...
- PE基础1
PE文件概述 文件格式 .png ..mp4..gif..dll等等,这些文件都具有不同格式 不能随意修改这些文件,否则将无法打开 PE文件(可执行文件) 学习PE文件目标 掌握PE文件就掌握wino ...
- hasOneOf # if (data.otherDescArr.some(_ => '7'.indexOf(_) > -1)) {
if (data.otherDescArr.some(_ => '7'.indexOf(_) > -1)) { export const hasOneOf = (targetarr, ar ...
- this.$Message.success('提示信息') 少写了一个c 导致报错
this.$Message.success('提示信息') 少写了一个c 导致报错 而且 $Message 输出还没显示,导致我以为是没有 $Message 对象了,其实全局对象直接调用即可
- flask学习规划
实现基本登录注册: 留言板功能: 并且部署到服务器: 预计完成时间6.16:. 后续页面美化: 各种其他功能的探索一个月时间: 并行地学习python,java,css,html,js,sql 暑假预 ...
- delphi中使用自定义资源的方法
如果要在delphi中使用自定义资源文件*.res文件,比如一个光标,此时可以采用下列步骤: 1,创建包含相应的资源文件,这里是创建一个包含自定义光标的res文件. 2,在主窗体的pas文件中加入编译 ...
- CPP-STL:vector容器
1. vector容器简介: vector向量容器是一种随机访问的数组类型,它提供了对数组元素的快速访问.随机访问,以及在序列尾部快速.随机地插入和删除操作.它类似于数据结构中的队列.数组和堆 ...
- loadClass和forName 的区别
类的加载方式 1.隐式加载:new 2.显式加载,loadClass,forName 等 loadClass 和 forName的区别 类的装载过程 1.加载:通过classLoader加载class ...