zoukankan      html  css  js  c++  java
  • Codeforces Round #298 (Div. 2) D. Handshakes [贪心]

    传送门

    D. Handshakes
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard 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.

    题意:

    一群小盆友挨个进入教室,与教室中每个没在做contest的小盆友握手。3个小盆友可以在任意时间开始一场不会结束的contest

    给出每个小盆友进教室时的握手次数。

    求一个进教室的次序,满足题意。

    题解:

    可以发现,握手次数,增只能一个一个增,减可以幅度很大

    故采取贪心策略,能增的情况就增(如果某个x在前面无法增加得到,后面就更无法达到了)

    10808859                 2015-04-21 13:41:24     njczy2010                     D - Handshakes                          GNU C++     Accepted 233 ms 122896 KB
      1 #include <cstdio>
      2 #include <cmath>
      3 #include <algorithm>
      4 #include <cstring>
      5 #include <vector>
      6 #include <queue>
      7 
      8 using namespace std;
      9 
     10 #define ll long long
     11 
     12 int const N = 200005;
     13 int const M = 1035;
     14 ll const mod = 1000000007;
     15 
     16 int n;
     17 int a[N];
     18 
     19 int cnt[N];
     20 int cou[4];
     21 int ans[N];
     22 int flag;
     23 queue<int> que[N];
     24 
     25 int judge()
     26 {
     27     if(n%3==0){
     28         if(cou[0]!=cou[1] || cou[0]!=cou[2] || cou[1]!=cou[2]){
     29             return 0;
     30         }
     31         else return 1;
     32     }
     33     else if(n%3==1){
     34         if(cou[0]!=cou[1]+1 || cou[0]!=cou[2]+1 || cou[1]!=cou[2]){
     35             return 0;
     36         }
     37         else return 1;
     38     }
     39     else{
     40         if(cou[0]!=cou[1] || cou[0]!=cou[2]+1 || cou[1]!=cou[2]+1){
     41             return 0;
     42         }
     43         else return 1;
     44     }
     45     return 1;
     46 }
     47 
     48 void solve()
     49 {
     50     int i,now;
     51     for(i=0;i<=n;i++){
     52         while(que[i].size()>=1) que[i].pop();
     53     }
     54     for(i=1;i<=n;i++){
     55         que[ a[i] ].push(i);
     56     }
     57     now=0;
     58     int te;
     59     for(i=1;i<=n;i++){
     60         while(now>=0){
     61             if(cnt[now]>0){
     62                 te=que[now].front();
     63                 que[now].pop();
     64                 ans[i]=te;
     65                 cnt[now]--;
     66                 now++;break;
     67             }
     68             now-=3;
     69         }
     70         //printf(" i=%d now=%d
    ",i,now);
     71         if(now<0){
     72             flag=0;break;
     73         }
     74     }
     75 }
     76 
     77 void out()
     78 {
     79     printf("Possible
    ");
     80     int i;
     81     printf("%d",ans[1]);
     82     for(i=2;i<=n;i++){
     83         printf(" %d",ans[i]);
     84     }
     85     printf("
    ");
     86 }
     87 
     88 int main()
     89 {
     90     //freopen("data.in","r",stdin);
     91     //scanf("%d",&T);
     92     //for(int ccnt=1;ccnt<=T;ccnt++)
     93     while(scanf("%d",&n)!=EOF)
     94     {
     95         memset(cou,0,sizeof(cou));
     96         memset(cnt,0,sizeof(cnt));
     97         int i;
     98         int j;
     99         flag=1;
    100         for(i=1;i<=n;i++){
    101             scanf("%d",&a[i]);
    102             j=a[i]%3;
    103             cnt[ a[i] ]++;
    104             cou[j]++;
    105         }
    106         flag=judge();
    107        // printf("  flag=%d
    ",flag);
    108         if(flag==0){
    109             printf("Impossible
    ");continue;
    110         }
    111         solve();
    112         if(flag==0){
    113             printf("Impossible
    ");continue;
    114         }
    115         out();
    116     }
    117     return 0;
    118 }
  • 相关阅读:
    LeetCode(123) Best Time to Buy and Sell Stock III
    LeetCode(122) Best Time to Buy and Sell Stock II
    LeetCode(147) Insertion Sort List
    360兼容模式不支持hidden属性的问题
    第一个博客,用来勉励自己,加油
    【LGR-059】洛谷7月月赛题解
    Codechef July Challenge 2019 Division 1题解
    AtCoder Grand Contest 035
    Comet OJ
    2019-7-3 感记
  • 原文地址:https://www.cnblogs.com/njczy2010/p/4445173.html
Copyright © 2011-2022 走看看