题目直通车:Codeforces 1027F. Session in BSU

思路:

对第一门考试,使用前一个时间,做标记,表示该时间已经用过,并让第一个时间指向第二个时间,表示,若之后的考试时间和当前第一个时间冲突时,可以找到当前第二个时间来代替

对每一门考试,如果前一个时间没有被使用过,直接用前一个时间,否则看前一个时间和后一个时间分别可以指向哪一个时间,假设指向x,y,看x和y的状态和大小,如果x,y都已经使用过,表示无解,否则的话,选择较小的,并更新时间指向的状态

时间的指向状态更新需要用 map离散数据并用并查集维护

#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<fstream>
#include<cstdlib>
#include<ctime>
#include<list>
#include<climits>
#include<bitset>
using namespace std;
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("input.in", "r", stdin);freopen("output.in", "w", stdout);
#define left asfdasdasdfasdfsdfasfsdfasfdas1
#define tan asfdasdasdfasdfasfdfasfsdfasfdas
typedef long long ll;
typedef unsigned int un;
const int desll[][]={{,},{,-},{,},{-,}};
const ll mod=1e9+;
const int maxn=2e6+;
const int maxm=1e6+;
const double eps=1e-;
int m,n;
int ar[maxn];
pair<int,int> pa[maxn];
map<int,int> ma;
bool wa[maxn];
int f[maxn];
int fin(int x)
{
return f[x]==x?x:f[x]=fin(f[x]);
}
int main()
{
scanf("%d",&n);
m=;
for(int i=;i<n;i++)scanf("%d%d",&pa[i].first,&pa[i].second),
ar[m++]=pa[i].first,ar[m++]=pa[i].second;
sort(ar,ar+m);
m=unique(ar,ar+m)-ar;
for(int i=;i<m;i++){
ma[ar[i]]=i;
}
for(int i=;i<m;i++)f[i]=i;
memset(wa,,sizeof(wa));int ans=;
for(int i=;i<n;i++){
int a=pa[i].first,b=pa[i].second;
if(!wa[ma[a]]){
wa[ma[a]]=;
ans=max(ans,a);
f[ma[a]]=ma[b];
continue;
}
int x=fin(ma[a]);
int y=fin(ma[b]);//分别寻找a,b指向的时间
if(wa[x] && wa[y]){
printf("-1\n");
return ;
}
if(wa[x]==false && wa[y]==false){
if(ar[x]<ar[y]){
f[x]=y;//更新时间指向的状态
wa[x]=;
ans=max(ans, ar[x]);
}
else{
f[y]=x;//更新时间指向的状态
wa[y]=;
ans=max(ans, ar[y]);
}
}
else if(!wa[x]){
wa[x]=;
ans=max(ans, ar[x]);
}
else{
wa[y]=;
ans=max(ans, ar[y]);
}
f[ma[a]]=y;//更新时间指向的状态
}
printf("%d\n",ans); return ;
}

Codeforces 1027F. Session in BSU的更多相关文章

  1. Codeforces 1027F Session in BSU - 并查集

    题目传送门 传送门I 传送门II 传送门III 题目大意 有$n​$门科目有考试,第$i​$门科目有两场考试,时间分别在$a_i, b_i\ \ (a_i < b_i)​$,要求每门科目至少参加 ...

  2. Codeforces.1027F.Session in BSU(思路 并查集)

    题目链接 \(Description\) 有\(n\)个人都要参加考试,每个人可以在\(ai\)或\(bi\)天考试,同一天不能有两个人考试.求最晚考试的人的时间最早能是多少.无解输出-1. \(So ...

  3. [Codeforces Round49F] Session in BSU

    [题目链接] http://codeforces.com/contest/1027/problem/F [算法] 二分图匹配 [代码] #include<bits/stdc++.h> #p ...

  4. CF 1027 F. Session in BSU

    F. Session in BSU https://codeforces.com/contest/1027/problem/F 题意: n场考试,每场可以安排在第ai天或者第bi天,问n场考完最少需要 ...

  5. codeforces1027F. Session in BSU

    题目链接 codeforces1027F. Session in BSU 题解 二分图匹配就fst了....显然是过去的,不过tle test87估计也pp了,好坑 那么对于上面做匹配的这个二分图分情 ...

  6. Session in BSU CodeForces - 1027F(思维 树 基环树 离散化)

    题意: 有n门考试,每门考试都有两个时间,存在几门考试时间冲突,求考完所有的考试,所用的最后时间的最小值 解析: 对于时间冲突的考试 就是一个联通块 把每个考试看作边,两个时间看作点,那么时间冲突的考 ...

  7. [Codeforces 1027 F] Session in BSU [并查集维护二分图匹配问题]

    题面 传送门 思路 真是一道神奇的题目呢 题目本身可以转化为二分图匹配问题,要求右半部分选择的点的最大编号最小的一组完美匹配 注意到这里左边半部分有一个性质:每个点恰好连出两条边到右半部分 那么我们可 ...

  8. [CF1027F]Session in BSU[最小基环树森林]

    题意 有 \(n\) 门课程,每门课程可以选择在 \(a_i\) 或者 \(b_i\) 天参加考试,每天最多考一门,问最早什么时候考完所有课程. \(n\leq 10^6\). 分析 类似 [BZOJ ...

  9. CF1027F Session in BSU

    link 花絮: 这场看起来打得还不错的样子……(别问我是用哪个号打的). 然后听说这题的思想被出了好多次,女生赛也出过,quailty算法,然而当时没反应过来,而且时间不多啦. 题意: 有n个人,每 ...

随机推荐

  1. 《算法》C++代码 前言

    现在大二正在上<数据结构>课,课内的书上代码实现很喜欢无脑用类.变量名字很长,而且常常实现太繁琐,并且代码有些无法运行,这些对于老手无所谓,但初学者看起来却会很不舒服.因此写点自己的代码, ...

  2. fiddler如何抓取夜神模拟器上的包

    一.设置Fiddler代理 1.点击Tools-Fiddler Options进入Fiddler Options页面 2.点击Connections,将Fiddler listens on port设 ...

  3. 【2017】KK English

    2017/11/24 Regardless of the enormous amount of photos shared on Wechat or Face book, modern city dw ...

  4. Python数据分析-Matplotlib图标绘制

    Matplotlib介绍 Matplotlib是一个强大的Python绘图和数据可视化的工具包. Matplotlib的主要功能 Matplotlib是python中的一个包,主要用于绘制2D图形(当 ...

  5. Spring 笔记(二)模块体系

    前言 在 Spring Boot 出现之前,开发一个 Spring 项目总会需要添加很多依赖.但是我们在配置依赖的时候,往往不太明确各依赖的具体作用,经常是从网上复制粘贴. 为何需要添加这些依赖?各依 ...

  6. 【Android】实验6 在应用程序中播放音频和视频 截止提交报告时间2016.4.21

    注:也可以在数独游戏项目中完成该实验的内容.

  7. 【bzoj3829】[Poi2014]FarmCraft 贪心

    原文地址:http://www.cnblogs.com/GXZlegend/p/6826667.html 题目描述 In a village called Byteville, there are   ...

  8. Icarus Verilog和GTKwave使用简析

    Icarus Verilog和GTKwave使用简析 来源 http://blog.csdn.net/husipeng86/article/details/60469543 本文测试文件在window ...

  9. CF 1103B Game with modulo

    题目 $a, x$ 是正整数.显然有 \begin{aligned} x \ge 2x \pmod{a} \implies a \le 2x \end{aligned} 若 $x \le a$ 则 \ ...

  10. ubuntu启动报错 Errors were found while checking the disk-drive for /

    开机报这个错误,主要原因是硬盘检测不通过导致的,下面介绍两种方法规避该问题: 修改grub 这个方法网上比较多,直接贴过来: 进入Ubuntu启动菜单时,光标选中 *Ubuntu 后,按键盘上的 e ...