Codeforces 1027F. Session in BSU
题目直通车: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的更多相关文章
- Codeforces 1027F Session in BSU - 并查集
题目传送门 传送门I 传送门II 传送门III 题目大意 有$n$门科目有考试,第$i$门科目有两场考试,时间分别在$a_i, b_i\ \ (a_i < b_i)$,要求每门科目至少参加 ...
- Codeforces.1027F.Session in BSU(思路 并查集)
题目链接 \(Description\) 有\(n\)个人都要参加考试,每个人可以在\(ai\)或\(bi\)天考试,同一天不能有两个人考试.求最晚考试的人的时间最早能是多少.无解输出-1. \(So ...
- [Codeforces Round49F] Session in BSU
[题目链接] http://codeforces.com/contest/1027/problem/F [算法] 二分图匹配 [代码] #include<bits/stdc++.h> #p ...
- CF 1027 F. Session in BSU
F. Session in BSU https://codeforces.com/contest/1027/problem/F 题意: n场考试,每场可以安排在第ai天或者第bi天,问n场考完最少需要 ...
- codeforces1027F. Session in BSU
题目链接 codeforces1027F. Session in BSU 题解 二分图匹配就fst了....显然是过去的,不过tle test87估计也pp了,好坑 那么对于上面做匹配的这个二分图分情 ...
- Session in BSU CodeForces - 1027F(思维 树 基环树 离散化)
题意: 有n门考试,每门考试都有两个时间,存在几门考试时间冲突,求考完所有的考试,所用的最后时间的最小值 解析: 对于时间冲突的考试 就是一个联通块 把每个考试看作边,两个时间看作点,那么时间冲突的考 ...
- [Codeforces 1027 F] Session in BSU [并查集维护二分图匹配问题]
题面 传送门 思路 真是一道神奇的题目呢 题目本身可以转化为二分图匹配问题,要求右半部分选择的点的最大编号最小的一组完美匹配 注意到这里左边半部分有一个性质:每个点恰好连出两条边到右半部分 那么我们可 ...
- [CF1027F]Session in BSU[最小基环树森林]
题意 有 \(n\) 门课程,每门课程可以选择在 \(a_i\) 或者 \(b_i\) 天参加考试,每天最多考一门,问最早什么时候考完所有课程. \(n\leq 10^6\). 分析 类似 [BZOJ ...
- CF1027F Session in BSU
link 花絮: 这场看起来打得还不错的样子……(别问我是用哪个号打的). 然后听说这题的思想被出了好多次,女生赛也出过,quailty算法,然而当时没反应过来,而且时间不多啦. 题意: 有n个人,每 ...
随机推荐
- Pascal游戏 大富翁MUD
大富翁MUD Pascal源码 Chaobs改编自百度网友作品 此源码非Chaobs原创,但转载时请仍注明出处. 估计90后以后就没有孩子知道MUD了. program wxtw; uses crt; ...
- 理解web缓存
web缓存是web用于临时存储各种资源的一种技术. web缓存大概分两种,一种是前端缓存,另一种是后端端缓存. 前端缓存 浏览器缓存 浏览器自带的缓存机制. 比如说浏览器后退前进的动作,一般使用浏览器 ...
- NGUI-使用UILabel呈现图片和不同格式的文字
1.可以使用BBCode标记 [b]Bold[/b] 粗体[i]italic[/i] 斜体[u]underli ...
- Entity Framework(一)
相关知识点复习: 1.var 类型推断: var p=new Person(); 2.匿名类型: var a=new {Name="wang",Age=12 }; 3.给新创建的 ...
- HDU 4346 The Beautiful Road ( 反向考虑 思路题 )
考虑对立情况,不漂亮的串的形式必然为GGGGR……R……RGGGG 相邻R之间的距离为奇数且相等. #include <cstdio> #include <cstring> # ...
- 软工实践 - 第十七次作业 Alpha 冲刺 (8/10)
队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/articles/10023469.html 作业博客:班级博客本次作业的链接 组员情况 组员1(队长 ...
- abp ef codefirst 设置默认值
public partial class tableIsWaringfiled : DbMigration { public override void Up() { //设置默认值为true Add ...
- BI商业智能培训系列——(一)概述
简介: Business Intelligence,简称 BI. 商业智能,是指用现代的技术进行数据分析,以实现商业价值,这些技术包括数据仓库技术,线上分析处理技术,数据挖掘,数据展现技术等. 以往的 ...
- 【bzoj3894】文理分科 网络流最小割
原文地址:http://www.cnblogs.com/GXZlegend 题目描述 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠结过) 小P所在的班级要进行文理分科.他的班级可以用 ...
- BZOJ1855 [Scoi2010]股票交易 【单调队列优化dp】
题目链接 BZOJ1855 题解 设\(f[i][j]\)表示第\(i\)天结束时拥有\(j\)张股票时的最大收益 若\(i \le W\),显然在这之前不可能有交易 \[f[i][j] = max\ ...