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配置三(插件minCssExtract/ DefinePlugin/Html)
(注:如无特殊说明这里的配置文件都指的是webpack.config.js) minCssExtract 我们通常期望js和js文件一起,css和css文件一起.该插件将CSS提取到单独的文件中.它为 ...
- Http接口安全设计
1. 完全开放 2. 基本验证 appid(企业唯一标识)+args(请求参数)->sign(摘要). 3. 时效控制 appid+args+timestamp(时间戳)->sign. ...
- itest(爱测试) 4.3.1 发布,开源BUG 跟踪管理 & 敏捷测试管理软件
4.3.0 发布后有三个用户强烈要求的更新,所以一周后4.3.1出炉,有点版本帝的味道哈,用户的反馈是我们持续升级的动力...... itest 简介:查看简介 test 开源敏捷测试管理,testO ...
- Type Java类型
参考:https://blog.csdn.net/a327369238/article/details/52621043 Type —— Java类型 Type是一个空接口,所有类型的公共接口(父接口 ...
- gulp 搭建静态服务器
步骤: 安装依赖:npm i browser-sync --save-dev 导入browser-sync,通过create创建 设置Sass和Js任务,将其压缩重命名并引入页面,任务结束时reloa ...
- 微服务介绍和springCloud组件
微服务架构模式是:将整个web服务 组织成一系列小的web 服务,这些小的web服务可以进行独立的编译和部署,并通过各自暴露的API接口 进行相互通信,他们相互协作,作为一个整体,为客户提供服务功 ...
- Linux 内核内存池
内核中经常进行内存的分配和释放.为了便于数据的频繁分配和回收,通常建立一个空闲链表——内存池.当不使用的已分配的内存时,将其放入内存池中,而不是直接释放掉. Linux内核提供了slab层来管理内存的 ...
- 在linux里面ps -ef | grep tomcat 什么意思
linux中的查看和tomcat有关系的进程的命令 例如 [root@localhost bin]# ps -eaf | grep tomcat root 5091 1 0 23:04 pts/0 0 ...
- ARM架构Linux环境安装python2.7.9
1.下载python # wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz 2.解压.编译安装 # tar -zxvf Pyt ...
- 台式机windows10 进入安全模式
按住shift键不松,在登录界面点击重启,即可进入安全模式!!!!