Codeforces 534D Handshakes(贪心)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Codeforces 534D Handshakes(贪心),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3056字,纯文字阅读大概需要5分钟。
内容图文
题意 房子里有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
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.
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.
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".
5 2 1 3 0 1
Possible 4 5 1 3 2
9 0 2 3 4 1 1 0 2 2
Possible 7 5 2 1 6 8 3 4 9
4 0 2 1 1
Impossible
原文:http://blog.csdn.net/acvay/article/details/45029813
内容总结
以上是互联网集市为您收集整理的Codeforces 534D Handshakes(贪心)全部内容,希望文章能够帮你解决Codeforces 534D Handshakes(贪心)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。