一、题目回顾
题目背景
快 noip 了,yyy 很紧张!
题目描述
现在各大 oj 上有 n 个比赛,每个比赛的开始、结束的时间点是知道的。
yyy 认为,参加越多的比赛,noip 就能考的越好(假的)。
所以,他想知道他最多能参加几个比赛。
由于 yyy 是蒟蒻,如果要参加一个比赛必须善始善终,而且不能同时参加 2 个及以上的比赛。
输入格式
第一行是一个整数 n,接下来 n 行每行是 2 个整数 , (),表示比赛开始、结束的时间。
输出格式
一个整数最多参加的比赛数目。
二、代码与思路
#include<bits/stdc++.h>
using namespace std;
int ans=1;//定义ans(一定要是一)
const int N=1e6+10;//定义常量
struct cat
{
int l;
int r;
};//定义结构体
cat a[N];//定义
bool cmp(const cat& a,const cat& b)
{
if(a.r!=b.r)return a.r<b.r;
return a.l<b.l;
}//排序cmp函数
int line(const cat& a,const cat& b)
{
return min(a.r,b.r)-max(a.l,b.l);
}//line计算函数
int main()
{
int n;//定义
cin>>n;//输入n
for(int i=1;i<=n;i++)
{
cin>>a[i].l>>a[i].r;//输入n组数据
}
sort(a+1,a+n+1,cmp);//排序
int last=1;//定义
for(int i=2;i<=n;i++)
{
if(line(a[i],a[last])<=0)//判断
{
ans++;//ans加一
last=i;//上一个变成第i个
}
}
cout<<ans;//输出
return 0;
}