Codeforces 534D Handshakes(贪心)

题意   房子里有n个人走进来  编号1~n  每个人走进来时房子里所有空闲的人都会和他招手  空闲的某三个人可以选择一起去打比赛  当然打比赛就变得不空闲了  给你每个人进来时和他招手的人的数量  要求输出一种可能的进房间顺序 没有可能的就输出Impossible

这题放在d就比较简单了  直接贪心就可以  把招手数量为i对应的人都保存到栈s[i]里  第一个进房间的人肯定是s[0]里的  然后让i从0开始  s[i]非空时  让s[i]的栈顶的人进入房间  然后i++  否则让s[i-1],s[i-2],s[i-3]都出栈一个 再让i=i-3 也就是有3个人去打比赛了  一直这样循环下去直到所有人都进入房间或者i<0(对应不可能的情况)  最后输出进入房间的序列就行了

#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
        {
            //出栈三个人去打比赛
            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 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 integersa1,?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
时间: 2024-11-06 21:47:54

Codeforces 534D Handshakes(贪心)的相关文章

Codeforces 534D Handshakes 构造 模拟 贪心

题意:人们依次进大厅,后进来的人会和里面所有的人都握手, 大厅里面有三个人就 其中丧二恩就可以结伴走出大厅.给你每个人进大厅时候握手的次数.让你求一个进场顺序. 解题思路:比赛的时候是用的从后往前推.比较难,发现从前往后直接模拟就行了 . 解题代码: 1 // File Name: d.cpp 2 // Author: darkdream 3 // Created Time: 2015年04月13日 星期一 01时30分17秒 4 5 #include<vector> 6 #include&l

Codeforces 534D - Handshakes(构造,暴力)

题意:总共有N个人进入教室,   每次进入的时候会和教室里面原有的人握手,并且记录握手的次数,当教室里面有大于等于三个人的时候,任意三个人可以组队去比赛(后面近来的人将无法看到他们), 现在给出人数N    给出乱序的人的握手次数,问是否可以组成一个合法的序列 题解: 一个人一个人往教室里走,  每次看看当前握手次数的询问是否还有,  如果还有 直接用当前握手次数否则将退回当前次数减3的次数继续查询,当查询合法后将序号放入数组存下.当查询到某个次数减到负数之后退出,查询数组长度是否大于N 代码:

Codeforces 413C Jeopardy!(贪心)

题目链接:Codeforces 413C Jeopardy! 题目大意:给出n个关卡,每个关卡闯关成功会得到相应的分数,有m个关卡闯关成功之后,可以选择不加上该关卡的分,而是将已有的分数翻倍,现在有一位选手已经有能力闯过所有的关卡,问说他能得到的最大分数是多少. 解题思路:贪心,将可以翻倍的关卡放在后面比,不能翻倍的关卡放在前面比,然后在按照关卡分数大的先比,如果该关卡分数可以翻倍,就判断是当前关卡的分数高还是已有的分数高. #include <cstdio> #include <cst

CodeForces - 95B(搜索+贪心)

题目链接:http://codeforces.com/problemset/problem/95/B 题意:给一个正整数n(1-100000位),求出不小于n的最小幸运.幸运数的概念是:由数量相等的4和7组成的数. 思路: 大体分三种情况: 1.n的位数len为奇数,最简单就是增加一位变成len+1偶数个,前一半为4,后一半为7 2.n的位数len为偶数,但找不到有len位数的幸运数比n大,那么就要增加两位len+2,前一半为4,后一半为7(可以和情况1放在一起) 3.n的位数len为偶数,可以

CodeForces 767E(贪心)

CodeForces 767E 题意:有100元的纸币和1元的硬币,某同学有无限多的纸币和 m 个硬币,并决定接下来的 n 天去食堂每天花费 c[i] 元.已知食堂大叔在第 i 天找零 x 元的话,不满意度会增加 w[i],问最小不满意度. 题解:按顺序先直接使用手上有的硬币,当手上硬币剩余为负数的时候,说明前面一定会出现有一天需要多使用一张纸币,取的策略就是取前面的代价 w[i]*(100-a[i]%100) 最少的一次,然后手里剩余硬币 +100.用优先队列维护即可. (代码略挫) 1 #i

Codeforces 452D [模拟][贪心]

题意: 给你k件衣服处理,告诉你洗衣机烘干机折叠机的数量,和它们处理一件衣服的时间,要求一件衣服在洗完之后必须立刻烘干,烘干之后必须立刻折叠,问所需的最小时间. 思路: 1.按照时间模拟 2.若洗完的衣服或者烘干的衣服较多来不及进行下一个步骤,则从一开始就顺延洗衣服的时间,贪心的思想也是体现在这里. 3.关键在于烘干衣服的顺延如何处理,因为需要调整洗衣服的起始时间,其实我们只要对烘干衣服的时间进行顺延处理就可以了,因为即使没有调整洗衣服的起始时间,那么下次到了烘干衣服的时间的时候因为烘干衣服的数

CodeForces 651A Joysticks 贪心

A. Joysticks time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Friends are going to play console. They have two joysticks and only one charger for them. Initially first joystick is charged at

Codeforces 484A Bits(贪心)

题目链接:Codeforces 484A Bits 题目大意:给定区间l,r,找到一个数x,保证x在区间上,并且要求x的bitcount尽量大的前提下数值尽量小. 解题思路:默认x为全1的二进制数,每次从最高为判断,看最高位的1变为0后大于r,就将该为变成0:落在区间上则即 为要照的答案:小于l则表示该为不能为0. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm&g

CodeForces #100 C 贪心+STL

题目链接:CodeForces #100  C 题意:现在给出n个snowball的半径,3个半径严格递增或递减的snowball,可以组成1个snowmen.问最多能组成多少个snowmen.并且按照半径递减的顺序输出每个snowmen的组成. 思路:嗯...每次都从前三个个数最多的snowball里选择,最后组成的snowmen最多... ...可以用优先队列写..但是感觉set+map写的太优雅了...map当然不等于数组了...哼. #include <stdio.h> #includ