#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T> inline void gmax(T &a,T b){if(b>a)a=b;}
template <class T> inline void gmin(T &a,T b){if(b<a)a=b;}
const int N=5e5+10,M=0,Z=1e9+7,ms63=1061109567;
int n,l,r;
pair<int,int>a[N];
int main()
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;++i)scanf("%d%d",&a[i].first,&a[i].second);
sort(a+1,a+n+1);
int ed=0;a[0].first=0;
int ans=0;
for(int i=1;i<=n;i++)if(a[i].first>a[i-1].first)
{
if(a[i].first>ed)
{
++ans;
ed=a[i].second;
}
else gmin(ed,a[i].second);
}
printf("%d\n",ans);
}
return 0;
}
/*
【trick&&吐槽】
不是所有的xe5都是1e5,这道题就是5e5.我的数组却开小了QwQ好难过!
【题意】
给你n(5e5)个区间,
希望选取最多个数的区间,
使得所有区间相互之间不覆盖。
并输出这个最多区间的区间数。
【类型】
贪心
【分析】
对于乱序的区间我们很难思考。
于是按照(first-左界升序,second-右界升序)的方式排序。
排序后我们顺序扫描,
首先,如果左界相同,所有区间最多只能选一个,我们肯定贪心选择右区间最小的那个。
然后,如果这个区间的左界与上一个左界不相同,我们会先看上个区间是否已经end,
如果end了,那么我们可以选择这个区间。
如果没end,那么这两个区间 构成冲突,它们的前效性是相同的,对之间的决策都不影响,都可以选取当前最多的区间数。
于是我们会贪心选择右边界小的那个。
于是,对于这道题,我们就可以用O(nlogn)的时间复杂度AC掉啦。
【时间复杂度&&优化】
O(nlogn)
*/
【Codeforces Testing Round 12B】【贪心】Restaurant 选取数量最多的不覆盖区间数
最新推荐文章于 2019-05-18 16:15:59 发布