Codeforces Round #611 (Div. 3) C
There are nn friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself.
For each friend the value fifi is known: it is either fi=0fi=0 if the ii-th friend doesn't know whom he wants to give the gift to or 1≤fi≤n1≤fi≤n if the ii-th friend wants to give the gift to the friend fifi.
You want to fill in the unknown values (fi=0fi=0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory.
If there are several answers, you can print any.
自己的暴力做法是:将所有人分成三类。第一类:有送有收。第二类:有送无收。第三类:无送有收。第四类:无送无收。
首先建立一个优先队列用于输出,第一类已经彻底明确了的直接丢进去。易知第二类和第三类人数应该是一样的,这时候讨论第四类:
1.若第四类人数为0:
只需要将第二类的收和第三类的送对应起来。一一对应即可。
2.若第四类人数为1:
第二类第三类前面n-1对一一对应,最后一对之间插一个唯一的第四类。(因为要求不能自己送自己)。
3.第四类人数大于1:
第四类内部对应即可,内部构成一个环。
#include<bits/stdc++.h>
using namespace std;
int n; struct point
{
int num;
int to;
int from;
}f[]; bool operator<(point a, point b){ return a.num>b.num ;} priority_queue<point> q;
vector<point> v1,v2,v3;//v1仅有from v2仅有to v3双无
int main()
{
cin>>n;
int i;
for(i=;i<=n;i++)
{
f[i].num=i;
f[i].to=;
f[i].from=;
} for(i=;i<=n;i++)
{
int temp;
scanf("%d",&temp);
f[i].to=temp;
f[temp].from=i;
} for(i=;i<=n;i++)
{
if(f[i].from!=&&f[i].to!=)
{
q.push(f[i]);
}
else if(f[i].from==&&f[i].to!=)
{
v2.push_back(f[i]);
}
else if(f[i].from!=&&f[i].to==)
{
v1.push_back(f[i]);
}
else
{
v3.push_back(f[i]);
}
} //cout<<v1.size()<<' '<<v2.size()<<' '<<v3.size()<<' '<<q.size()<<endl; if(v3.size()!=)
{
for(i=;i<v2.size();i++)
{
v2[i].from=v1[i].num;
v1[i].to=v2[i].num;
q.push(v1[i]);
q.push(v2[i]);
}
if(v3.size()==)goto label;
for(i=;i<v3.size()-;i++)//注意 不能自己给自己
{
v3[i].to=v3[i+].num;
q.push(v3[i]); }
v3[v3.size()-].to=v3[].num;
q.push(v3[v3.size()-]);
}
else
{
for(i=;i<v2.size()-;i++)
{
v2[i].from=v1[i].num;
v1[i].to=v2[i].num;
q.push(v1[i]);
q.push(v2[i]);
}
v2[v2.size()-].from=v3[].num;
v1[v2.size()-].to=v3[].num;
v3[].to=v2[v2.size()-].num;
q.push(v1[v2.size()-]);
q.push(v2[v2.size()-]);
q.push(v3[]); }
label:;
while(!q.empty())
{
point temp=q.top();
cout<<temp.to<<' ';
q.pop();
} return ;
}
Codeforces Round #611 (Div. 3) C的更多相关文章
- Codeforces Round #611 (Div. 3) A-F简要题解
contest链接:https://codeforces.com/contest/1283 A. Minutes Before the New Year 题意:给一个当前时间,输出离第二天差多少分钟 ...
- Codeforces Round #611 (Div. 3)
原题面:https://codeforces.com/contest/1283 A.Minutes Before the New Year 题目大意:给定时间,问距离零点零分还有多久? 分析:注意一下 ...
- Codeforces Round #611 (Div. 3) E
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past ...
- Codeforces Round #611 (Div. 3) D
There are nn Christmas trees on an infinite number line. The ii -th tree grows at the position xixi ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
随机推荐
- webpack4.41.0配置四(热替换)
每次修改都要去编译,这个操作比较繁琐.所以我们希望编译过程是自动化的,而且页面的更新也是自动化的.所以需要使用这个热替换 1.首先安装webpack-dev-server:npm install w ...
- codeforces C. Primes and Multiplication(快速幂 唯一分解定理)
题目链接:http://codeforces.com/contest/1228/problem/C 题解:给定一个函数f,g,题目有描述其中的表达式含义和两者之间的关系. 然后计算: 首先把给定的x用 ...
- 第二十六篇 玩转数据结构——二分搜索树(Binary Search Tree)
1.. 二叉树 跟链表一样,二叉树也是一种动态数据结构,即,不需要在创建时指定大小. 跟链表不同的是,二叉树中的每个节点,除了要存放元素e,它还有两个指向其它节点的引用,分别用Node l ...
- JS json对象(Object)和字符串(String)互转方法
[JS json对象(Object)和字符串(String)互转方法] 参考:https://blog.csdn.net/wenqianla2550/article/details/78232706 ...
- js语言简介
JS语言概述 JS语言简史 JS语言的起源 网景(Netscape Communication Corperation),1994年,推出第一款商用浏览器,网景浏览器(Netscape Navigat ...
- 使用js为表格添加一行
今天同事问了我一个问题,为表格添加新的行,我用的js写了一下,代码如下: <!DOCTYPE html> <html> <head> <meta charse ...
- 【游戏体验】Infiltraing the Airship(火柴人潜入飞船)
这款作品的游戏性非常高 而且很多地方都是玩梗 不乏趣味和幽默 推荐试玩 个人测评 游戏性 10/10 音乐 9/10 剧情 8/10 总评 27/30
- 2019冬季PAT甲级第三题
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ]; ]; ]; int main(){ ...
- java调用第三方接口(转载)
在很多时候有些别人做过的东西直接拿来用就好了,例如:身份证的信息.某个地区的天气.电话归属地等等. 代码 //import com.alibaba.fastjson.JSONObject; impor ...
- 运行composer出现do not run Composer as root/super user!
curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer composer - ...