Codeforces 534D Handshakes (greedy)

Source: Internet
Author: User

Test Instructions House There are N people came into the number 1~n everyone walked in the house all the free people will and he waved to a certain three free people can choose to play together, of course, the game will become not free of the number of people who come in and wave at him. The quantity of the person to whom he beckons is required to output a possible incoming order without possible loss. Out impossible

This problem in D is relatively simple direct greed can be waved to the number of I corresponding to the people are saved to stack s[i] in the first person into the room must be s[0] and let I start from 0 s[i] non-empty when let S[i] the top of the stack of people into the room and i++ otherwise let S[i-1],s[i-2] , s[i-3] are out of the stack one more let i=i-3 that is, there are 3 people to play the game has been such a cycle until everyone enters the room or i<0 (corresponding to the impossibility of the case) the final output into the room sequence on the line

#include <bits/stdc++.h>using namespace Std;const int N = 2e5 + 5;int q[n], n;stack<int> s[n];int main () {    int A;    scanf ("%d", &n);    for (int i = 1; I <= n; ++i)        scanf ("%d", &a), S[a].push (i);    int p = 0, i = 0;    while (1)    {        if (!s[i].empty ())  q[p++] = S[i++].top ();        else        {            //out of stack three people go to play            if (I < 3) break;            S[--i].pop (), S[--i].pop (), S[--i].pop ();        }    }    if (p = = N)    {        puts ("Possible");        for (int i = 0; i < n-1; ++i)            printf ("%d", Q[i]);        printf ("%d\n", q[n-1]);    }    Else puts ("impossible");    return 0;} Last modified:   2015-04-13 01:57

D. Handshakes

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 under at he desk, greeted with those who were present in the hostel by shaking hand S. Each of the students who came in stayed in Ctop until the end of the day and never left.

At no time any three students could join together and start participating in a team contest, which lasted until the end O f the day. The team didn't distract from the contest for a minute, so when another student came in and greeted those who were presen T, he did not shake hands and the members of the contest writing team. Each team consisted the 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 all student, get a possible order in which the students could has come To Ctop. If Such an order does not exist and then print the This is impossible.

Please note this some students could work independently until the end of the day, without participating in a team contest.

Input

The first line contains integerN(1?≤? n? ≤?2 105 )-the number of students who came to ctop. The next line containsNIntegers a1,? a 2,?...,? a N (0?≤? a i? < N ), where ai Is the number of students with theI-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 T He center. NumberIThat stands to the left of numberJIn this permutation means theI-th student came earlier than theJ-th student. If There is multiple answers, print any of them.

If the sought order of students doesn ' t exist, in a single line print "impossible".

Sample Test (s) input
52 1 3) 0 1
Output
Input
90 2 3 4 1 1 0 2 2
Output
Possible7 5 2 1 6 8 3 4 9
Input
40 2 1 1
Output
Impossible


Codeforces 534D Handshakes (greedy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.