Codeforces-534D Handshakes

传送门

N个数,第i个数代表i之前的还未参加比赛的人的人数。每来够三个人可以参加比赛,她们可以随时加入比赛(也可以暂时不加入),求一个可能的到达顺序满足题意

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <vector>
 5 #define INF 0x3f3f3f3f
 6 #define MOD 1000000007
 7 using namespace std;
 8 typedef long long LL;
 9
10 const int maxn = 2e5 + 10;
11 int ans[maxn];
12 int cnt[maxn];
13 int num[maxn];
14 vector<int> G[maxn];
15
16 int nxt(int x) {
17     int up = x - 3, down = x % 3;
18     while (up > down) {
19         if (cnt[up]) return up;
20         up -= 3;
21     }
22     return down;
23 }
24
25 int main() {
26     int N;
27     scanf("%d", &N);
28     for (int i = 1; i <= N; i++) {
29         scanf("%d", &num[i]);
30         G[num[i]].push_back(i);
31         cnt[num[i]]++;
32     }
33     bool flag = 1;
34     int k = 0;
35     while (1) {
36         if (ans[0] == N) break;
37         if (cnt[k]) {
38             flag = 1;
39             int tmp = G[k][--cnt[k]];
40             ans[++ans[0]] = tmp;
41             k++;
42         } else {
43             k = nxt(k);
44             if (flag == 0) break;
45             flag = 0;
46         }
47     }
48     if (ans[0] == N) {
49         puts("Possible");
50         for (int i = 1; i <= ans[0]; i++) {
51             printf("%d%c", ans[i], i == ans[0] ? ‘\n‘ : ‘ ‘);
52         }
53     } else {
54         puts("Impossible");
55     }
56     return 0;
57 }
时间: 2024-08-05 08:25:31

Codeforces-534D Handshakes的相关文章

Codeforces 534D Handshakes(贪心)

题意   房子里有n个人走进来  编号1~n  每个人走进来时房子里所有空闲的人都会和他招手  空闲的某三个人可以选择一起去打比赛  当然打比赛就变得不空闲了  给你每个人进来时和他招手的人的数量  要求输出一种可能的进房间顺序 没有可能的就输出Impossible 这题放在d就比较简单了  直接贪心就可以  把招手数量为i对应的人都保存到栈s[i]里  第一个进房间的人肯定是s[0]里的  然后让i从0开始  s[i]非空时  让s[i]的栈顶的人进入房间  然后i++  否则让s[i-1],

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

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

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 Program B

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 tho

Codeforce 水题报告(2)

又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数(n<=200). 首先很明显可能是区间dp,我们可以记f[i][j]为从i到j的这个多边形的三角剖分数,那么f[i][j]=f[i][k]*f[j][k]*(i,j,k是否为1个合格的三角形) CodeForces 438D:The Child and Sequence 描述:给一个序列,要求支持区

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store