【codeforces - 534D】 - Handshakes

原题链接 

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

Sample Input

Input

5
2 1 3 0 1

Output

Possible
4 5 1 3 2 

Input

9
0 2 3 4 1 1 0 2 2

Output

Possible
7 5 2 1 6 8 3 4 9

Input

4
0 2 1 1

Output

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.

题目大意: 

n个人依次进入一个房间,每个人进入时只会主动和没有组队的人握手,那么此刻握手的次数即为这个人握手的次数(即后面进来的人与某个人握手,并不会增加某个人的握手次数),任意时刻任意三人能够组成一队,例如,在第四个人进来之前,前三个人组队,那么第四个人的握手次数为0,最后可以有人没组队(即组队任意的,可以组也可以不组),现在给你每个人的握手信息,给出这n个人进来的顺序,顺序不是唯一的,输出任意一种即可。

题目分析:

这题很明显要从握手次数为0的开始进行,我一开始也想到这个,但我是把握手次数进行处理,然后贪心的先把0, 1,2进行处理,例如0, 2, 3, 4, 1, 1, 0, 2, 2,这样的握手信息我按照0, 1, 2, 0, 1, 2, 3, 4, 2这样输出对应的人, 这样写看起来是对的,但是这样贪心是错的, 即使类似的样例都是对的,但是像这种3, 0, 0, 4, 2, 2, 1,刚才的方法就会发现无解,因为这种发现忽略了前面的队尾在中间组队的情况,会使解的可能性减小,刚才的样例正确的解是0, 1, 2, 3, 4, 2, 0,由此我们可以想到正确的贪心做法:我们首先看有没有次数为0的, 其次看次数为1的, 2的, 3的, 直到发现没有次数为k的, 然后我们组一个队, 看有没有次数k - 3的,如果还没有, 就在组一个队,即k - 6次的,直到所要找的次数小于0, 如果其中某个时刻,人数到达n,那么就是可解的,如果最后要找的次数小于0,则无解。

那么为什么这样是对的呢?因为在第i个人进去前组的队在第i个人进去之前任意时刻组队都是一样的, 这不会导致解的可能性减少,因此我们可以在我们需要的时候组队,如果握手信息是有解的,那么我们一定可以模拟出可行解。

而且我们发现题目的样例和我们用vector模拟出来的结果一样(题目样例的答案一般都是标程跑出的,或许我们可以利用这一点进行思考)

AC代码:

#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
const int N = 2e5 + 5;
vector <int> v[N];
int ans[N];
int main()
{
    int n;
    while(scanf("%d", &n) != EOF){
        int x;
        for (int i = 1; i <= n; i ++){
            scanf("%d", &x);
            v[x].push_back(i);
        }
        int num = 0;
        int f = 0;
        for (int i = 1; i <= n; i ++){
            while (num >= 0 && v[num].size() == 0)num = num - 3;
            if (num < 0){
                f = 1;
                break;
            }
            ans[i] = v[num].back();
            v[num ++].pop_back();
        }
        if (f)printf("Impossible\n");
        else{
            printf("Possible\n");
            for (int i = 1; i < n; i ++)printf("%d ", ans[i]);
            printf("%d\n", ans[n]);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值