CodeForces 534D Handshakes 【STL】+【贪心】

D. Handshakes time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard 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.

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 test(s) 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 Note 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个人,,按照一定的次序进入房间,进去后要给没组成对(三人一队)的人挥手,例如房间中已经有5个人并且有三个人已经组成队了,那么进来的人需要挥两次手,问题是给你这n个人的挥手次数,找出一种满足挥手次数的进房间次序(可能有多种,看例)。 例: 5 2 1 3 0 1 那么可能的顺序是 4先进 (a4 = 0) 5再进(a5 = 1) 与4挥手 1再进(a1 = 2)与4,5挥手 3再进(a3 = 3) 与1, 4, 5挥手 然后1 4 5 组成一队 2再进(a2 = 1)与3挥手 所以次序可能为4 5 1 3 2(虽然与题例不同,但满足条件) 思路:将每一个挥手次数为i的都放到栈S[i]中,刚开始肯定是从0开始,(结束条件是全部进房间或者是i<0)让i=0,如果s[i]不为空,那么就i++(表示能取到当前的数),否则的话,如果i<0就跳出,否则就将i-1,i-2,i-3栈依次出栈顶(表示他们都去组队了)。最后判断进房间的是不是n个人就好了。我是用vector放答案的,所以最后判断vector的长度 代码:

;const int M = 2e5+5;stack<int > s[M];vector<int > ans;int main(){int n, temp;while(~scanf(“%d”, &n)){int i = 0;while(i < n){while(!s[i].empty()) s[i].pop(); ++i;}ans.clear();for(i = 1; i <= n; ++ i){scanf(“%d”, &temp); s[temp].push(i);}i = 0;while(1){if(!s[i].empty()){ans.push_back(s[i].top());++i;}else{if(i < 3) break;for(int j = 1; j <= 3; ++ j) s[i-j].pop();i -= 3;}}if(ans.size() == n){printf(“Possible\n”);for(int i = 0; i < ans.size(); ++ i) printf(“%d%c”, ans[i], i+1 == n?’\n’:’ ‘);}else printf(“Impossible\n”);}return 0;}

接受失败等于放松自己高压的心理,

CodeForces 534D Handshakes 【STL】+【贪心】

相关文章:

你感兴趣的文章:

标签云: