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.

大意是有n个人相互给礼物(限定一个人只能收到一份,赠送一份),已知其中一部分人希望把礼物送给谁,要求补全这个关系图,只需要输出任何一种可能的情况。其中不能自己送给自己礼物。
自己的暴力做法是:将所有人分成三类。第一类:有送有收。第二类:有送无收。第三类:无送有收。第四类:无送无收。
首先建立一个优先队列用于输出,第一类已经彻底明确了的直接丢进去。易知第二类和第三类人数应该是一样的,这时候讨论第四类:
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的更多相关文章

  1. Codeforces Round #611 (Div. 3) A-F简要题解

    contest链接:https://codeforces.com/contest/1283 A. Minutes Before the New Year 题意:给一个当前时间,输出离第二天差多少分钟 ...

  2. Codeforces Round #611 (Div. 3)

    原题面:https://codeforces.com/contest/1283 A.Minutes Before the New Year 题目大意:给定时间,问距离零点零分还有多久? 分析:注意一下 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  7. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  8. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  9. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

随机推荐

  1. [USACO08JAN]Haybale Guessing(LuoguP2898)

    The cows, who always have an inferiority complex about their intelligence, have a new guessing game ...

  2. MySQL学习(六)change-buffer

    文章部分总结描述来自参考文章,属于半原创. 概述     文章将会介绍 change buffer 相关的知识点 查看 MySQL InnoDB 状态的命令 SHOW ENGINE INNODB ST ...

  3. windows 安装 MySQL

    windows 安装 MySQL MySQL 目录结构 成功完成 MySQL 数据库的安装和配置!

  4. css transform 2D3D转换

    2D转换 translate 移动 <style> div{ width: 100px; height: 100px; } .box{ border: 1px dashed red; fl ...

  5. Codeforces Round #622 C2.Skyscrapers (hard version)

    This is a harder version of the problem. In this version n≤500000n≤500000 The outskirts of the capit ...

  6. 【笔记】Linux进程间同步和进程绑定至特定cpu

    #define _GNU_SOURCE #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> ...

  7. C#委托和事件的区别

    “委托是具有相同签名的函数(方法)的类型,事件是委托的应用方式之一” ---来自评论区老司机 delegate 是为了在C#中把函数作为对象传来传去而实现的一个“函数包装”.由于在C#中函数是二等公民 ...

  8. Subroutine 子程序 Perl 第四章

    子程序的定义是全局的,不需要事先声明.若重复定义子程序,后面的覆盖前面的. sub marine { $n +=1; print " Hello ,sailor number $_ ! &q ...

  9. red hat 报错:apt-get:找不到命令

    Linux有两个系列:一个是RedHat系列,一个是Debian系列. RedHat系列:Redhat.Centos.Fedora等 Debian系列:Debian.Ubuntu等 RedHat 系列 ...

  10. ASP.NET Core搭建多层网站架构【0-前言】

    2020/01/26, ASP.NET Core 3.1, VS2019 摘要:基于ASP.NET Core 3.1 WebApi搭建后端多层网站架构 目录 0-前言 1-项目结构分层建立 2-公共基 ...