首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【CodeForces】597B - Restaurant(贪心)

【CodeForces】597B - Restaurant(贪心)

作者头像
FishWang
发布2025-08-26 15:27:57
发布2025-08-26 15:27:57
10900
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

. 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.

Examples

input

代码语言:javascript
代码运行次数:0
运行
复制
2
7 11
4 7

output

代码语言:javascript
代码运行次数:0
运行
复制
1

input

代码语言:javascript
代码运行次数:0
运行
复制
5
1 2
2 3
3 4
4 5
5 6

output

代码语言:javascript
代码运行次数:0
运行
复制
3

input

代码语言:javascript
代码运行次数:0
运行
复制
6
4 8
1 5
4 7
2 5
1 3
6 8

output

代码语言:javascript
代码运行次数:0
运行
复制
2

经典的贪心算法的题,和hdu的《今年暑假不AC》一样。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <algorithm>
using namespace std;
struct node
{
	int st,endd;
}data[500000+11];
bool cmp(node a,node b)
{
	return a.endd < b.endd;
}
int main()
{
	int n;
	while (~scanf ("%d",&n))
	{
		for (int i = 0 ; i < n ; i++)
			scanf ("%d %d",&data[i].st,&data[i].endd);
		sort(data,data+n,cmp);
		int pos = 0;
		int ans = 1;
		for (int i = 1 ; i < n ; i++)
		{
			if (data[i].st > data[pos].endd)
			{
				pos = i;
				ans++;
			}
		}
		printf ("%d\n",ans);
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档