【Codeforces 534D】【贪心】Handshakes

Handshakes

Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

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”.

Samples

Input1

5
2 1 3 0 1

Output1

Possible
4 5 1 3 2

Input2

9
0 2 3 4 1 1 0 2 2

Output2

Possible
7 5 2 1 6 8 3 4 9

nput3

4
0 2 1 1

Output3

Impossible

Hint

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.

Source

Codeforces Round #298 (Div. 2)

先说题意:有n 个人准备参加ACM 比赛,他们按一定顺序入场,每个人入场时会与已到场且未组队的人握手。在任意时刻已入场的三个未组队的人可以选择组队参加比赛,一个人只能属于一支队,一支队恰好三个人。现给出每个人入场时与其握手的人数,请你给出一个入场方案或指出无解

这个贪心的题很神奇,我只能这么说,贪心的思路就是能进就进,不能进就走3人,不能走人就impossible。
证明其实还是比较简单的,如果有一个人要握n次手,但是到他那里卡死了,要输出impossible了,所以这个时候场内人一定不是n+3*k,然后如果他在前面进来了,人数就加了一个,后面就一定不是n+3*k+1,而原来后面组成不了n+3*k,现在也不行,所以如果在前面就放入这个人就只有直接崩掉。
所以贪心正确
但是如果在考试完全不敢写QAQ,总感觉不对

然后下面是代码,写起来实际上很简单

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<stack>
#define INF 2100000000
#define ll long long
#define clr(x)  memset(x,0,sizeof(x))
#define clrmax(x)  memset(x,127,sizeof(x))

using namespace std;

inline int read()
{
    char c;
    int ret=0;
    while(!(c>='0'&&c<='9'))
        c=getchar();
    while(c>='0'&&c<='9')
    {
        ret=(c-'0')+(ret<<1)+(ret<<3);
        c=getchar();
    }
    return ret;
}

#define M 200005

int a[M],n,num,in,ans[M],sta;
vector<int>V[M];

int main()
{
    freopen("in.txt","r",stdin);
    n=read();
    for(int i=1;i<=n;i++)
    {
        a[i]=read();
        V[a[i]].push_back(i);
    }
    while(num<n)
    {
        if(!V[in].empty())
        {
            ans[++sta]=V[in][V[in].size()-1];
            V[in].pop_back();
            in++;
            num++;
        }
        else 
            if(in>=3)in-=3;
            else 
            {
                printf("Impossible");
                return 0;
            }   
    }
    printf("Possible\n");
    for(int i=1;i<=n;i++)
        printf("%d ",ans[i]);
    return 0;
}

大概就是这个样子,如果有什么问题,或错误,请在评论区提出,谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值