CF534D.Handshakes

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

2月30日,共有 nn 名学生陆续来到贝尔兰国立大学的奥林匹克编程培训中心(CTOP)。他们依次单独进入中心。每名学生在进入后、就座前,会与当时已在房间内的所有学生握手致意。所有进入的学生均会在 CTOP 一直待到当天结束,中途不会离开。

在任意时刻,任意三名学生均可组成一支队伍,开始参加团队竞赛;该竞赛将持续至当天结束。团队在整个竞赛过程中全神贯注、毫不分心,因此当后续有新学生进入并与其他在场者握手致意时,他不会与正在参加团队竞赛的队员握手。每支队伍恰好由三名学生组成,且每名学生最多只能加入一支队伍。不同队伍可在不同时刻开始竞赛。

已知每位学生进入时与其握手的在场人数,请给出一种可能的学生到达 CTOP 的顺序;若不存在这样的顺序,则输出“不可能”。

请注意:部分学生可能全程独立工作至当天结束,未参与任何团队竞赛。

输入格式

The first line contains integer n (1 ≤ n ≤ 2·105) — the number of students who came to CTOP. The next line contains n integers _a_1, _a_2, ..., a__n (0 ≤ a__i < n), where a__i is the number of students with who the i-th student shook hands.

第一行包含一个整数 nn(1≤n≤2⋅1051 \leq n \leq 2\cdot10^5)—— 表示参加 CTOP 的学生人数。
下一行包含 nn 个整数 a1, a2, ..., ana_1,\,a_2,\,...,\,a_n(0≤ai<n0 \leq a_i < n),其中 aia_i 表示第 ii 位学生与之握手的学生人数。

输出格式

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

如果所求的学生顺序存在,则在第一行输出“Possible”,在第二行输出学生编号的一个排列,该排列定义了学生进入中心的顺序。在此排列中,若数字 ii 位于数字 jj 的左侧,则表示第 ii 位学生比第 jj 位学生更早到达。若存在多个可行答案,输出任意一个即可。

如果所求的学生顺序不存在,则在单独一行输出“Impossible”。

输入输出样例

  • 输入#1

    5
    2 1 3 0 1

    输出#1

    Possible
    4 5 1 3 2
  • 输入#2

    9
    0 2 3 4 1 1 0 2 2

    输出#2

    Possible
    7 5 2 1 6 8 3 4 9
  • 输入#3

    4
    0 2 1 1

    输出#3

    Impossible

说明/提示

In the first sample from the statement the order of events could be as follows:

  • student 4 comes in (_a_4 = 0), he has no one to greet;
  • student 5 comes in (_a_5 = 1), he shakes hands with student 4;
  • student 1 comes in (_a_1 = 2), he shakes hands with two students (students 4, 5);
  • student 3 comes in (_a_3 = 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 (_a_2 = 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 (_a_7 = 0), he has nobody to greet;
  • student 5 comes in (_a_5 = 1), he shakes hands with student 7;
  • student 2 comes in (_a_2 = 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(_a_1 = 0), he has no one to greet (everyone is busy with the contest);
  • student 6 comes in (_a_6 = 1), he shakes hands with student 1;
  • student 8 comes in (_a_8 = 2), he shakes hands with two students (students 1, 6);
  • student 3 comes in (_a_3 = 3), he shakes hands with three students (students 1, 6, 8);
  • student 4 comes in (_a_4 = 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 (_a_9 = 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 (_a_1 = 0), he has no one to greet;
  • student 3 comes in (or student 4) (_a_3 = _a_4 = 1), he shakes hands with student 1;
  • student 2 comes in (_a_2 = 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 (_a_3 = _a_4 = 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.

在题面的第一个样例中,事件发生的顺序可能如下:

  • 学生 4 进入教室(a4=0a_4 = 0),他无人可问候;
  • 学生 5 进入教室(a5=1a_5 = 1),他与学生 4 握手;
  • 学生 1 进入教室(a1=2a_1 = 2),他与两名学生(学生 4、5)握手;
  • 学生 3 进入教室(a3=3a_3 = 3),他与三名学生(学生 4、5、1)握手;
  • 学生 4、5、3 组成一支队伍并开始编写竞赛题目;
  • 学生 2 进入教室(a2=1a_2 = 1),他与一名学生(编号为 1)握手。

在题面的第二个样例中,事件发生的顺序可能如下:

  • 学生 7 进入教室(a7=0a_7 = 0),他无人可问候;
  • 学生 5 进入教室(a5=1a_5 = 1),他与学生 7 握手;
  • 学生 2 进入教室(a2=2a_2 = 2),他与两名学生(学生 7、5)握手;
  • 学生 7、5、2 组成一支队伍并开始编写竞赛题目;
  • 学生 1 进入教室(a1=0a_1 = 0),他无人可问候(其余人正忙于竞赛);
  • 学生 6 进入教室(a6=1a_6 = 1),他与学生 1 握手;
  • 学生 8 进入教室(a8=2a_8 = 2),他与两名学生(学生 1、6)握手;
  • 学生 3 进入教室(a3=3a_3 = 3),他与三名学生(学生 1、6、8)握手;
  • 学生 4 进入教室(a4=4a_4 = 4),他与四名学生(学生 1、6、8、3)握手;
  • 学生 8、3、4 组成一支队伍并开始编写竞赛题目;
  • 学生 9 进入教室(a9=2a_9 = 2),他与两名学生(学生 1、6)握手。

在题面的第三个样例中,事件发生的顺序可被唯一还原:

  • 学生 1 进入教室(a1=0a_1 = 0),他无人可问候;
  • 学生 3(或学生 4)进入教室(a3=a4=1a_3 = a_4 = 1),他与学生 1 握手;
  • 学生 2 进入教室(a2=2a_2 = 2),他与两名学生(学生 1、3(或 4))握手;
  • 剩余的学生 4(或学生 3)必须与一名学生握手(a3=a4=1a_3 = a_4 = 1),但这是不可能的:因为仅存在两种情形——要么已组队,从而他不与任何人问候;要么他需与当前所有单独工作的三人全部握手。

输入解题思路,AI测评打分。不知道怎么写?

首页