【Codeforces Testing Round 12B】【贪心】Restaurant 选取数量最多的不覆盖区间数

B. Restaurant
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri).

Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?

No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.

Input

The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109).

Output

Print the maximal number of orders that can be accepted.

Sample test(s)
input
2
7 11
4 7
output
1
input
5
1 2
2 3
3 4
4 5
5 6
output
3
input
6
4 8
1 5
4 7
2 5
1 3
6 8
output
2

#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)
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值