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 ...
随机推荐
- Django_模型
1. ORM 2. 简单使用 3. 外键 2.0以上的版本要这样写s_grade = models.ForeignKey(Grade,on_delete=models.CASCADE) 3. 修改表名 ...
- ASP学习笔记1
一.变量 1.1 声明变量 dim name name="Donald Duck" response.write("My name is: " & na ...
- pyfits fits图像区域选择
在用pyfits读取fits格式的图像时,得到的数组的结构如下 f=pyfits.open('rr.fits') data1=f[0].data data1数组的第一行,对应于图像的最下面一行,数组第 ...
- 使用webpack搭建vue环境
1.安装node.js,在官网下载,直接下一步,完成.nodejs里默认包含npm环境.国内安装包的速度太慢,建议使用cnpm淘宝镜像. npm install -g cnpm --registry= ...
- IntelliJ IDEA 2017.3尚硅谷-----界面展示
- (转)JSONObject的toBean 和 fromObject
public static void main(String[] args) { Map map=new HashMap();map.put("我","妹"); ...
- 微服务介绍和springCloud组件
微服务架构模式是:将整个web服务 组织成一系列小的web 服务,这些小的web服务可以进行独立的编译和部署,并通过各自暴露的API接口 进行相互通信,他们相互协作,作为一个整体,为客户提供服务功 ...
- PHP把空格、换行符、中文逗号等替换成英文逗号的正则表达式
$test=$_POST["test"]; $test= preg_replace("/(\n)|(\s)|(\t)|(\')|(')|(,)/" ,',' , ...
- Yii2手动安装第三方扩展
对于没有进入composer的扩展,请通通将他们下载到vendor内. 然后,打开vendor/yiisoft/extensions.php 文件,在里面的数组里增加一项,如下面代码 'SDK/Lvb ...
- 【C语言】写一个函数,并调用该函数求两个整数的最大公约数和最小公倍数
程序分析: 在数学中,两个数的最小公倍数=两个数的乘积/两数的最大公约数. 求两个数的最大公约数,运用辗转相除法:已知两个整数M和N,假定M>N,则求M%N. 如果余数为0,则N即为所求:如果余 ...