CodeForces 597BRestaurant(饭店准备餐桌问题)

来源:互联网 发布:深圳淘宝化妆品销售 编辑:程序博客网 时间:2024/04/30 11:56

B. Restaurant

time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

A restaurant receivednorders for the rental. Each rental order reserve the restaurant for acontinuous period of time, the i-thorder is characterized by two time values — the start timeliand the finish timeri(li ≤ ri).

Restaurant managementcan accept and reject orders. What is the maximal number of orders therestaurant can accept?

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

Input

The first linecontains integer numbern (1 ≤ n ≤ 5·105)— number of orders. The following nlines contain integer valuesliandrieach (1 ≤ li ≤ ri ≤ 109).

Output

Print the maximal number of ordersthat can be accepted.

Examples

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<stdlib.h>#include<math.h>#include<string.h>#include<algorithm>#define MYDD 110300*5using namespace std;struct Q {int lai;int zou;} dd[MYDD];bool cmp_zou(Q x,Q y) {return x.zou<y.zou;}int main() {int n,ans,away;while(scanf("%d",&n)!=EOF) {for(int j=0; j<n; j++)scanf("%d%d",&dd[j].lai,&dd[j].zou);sort(dd,dd+n,cmp_zou);//for(int j=0; j<n; j++)//printf("******%d %d******\n",dd[j].lai,dd[j].zou);ans=0;away=0;for(int j=0; j<n; j++) {if(away<dd[j].lai) {away=dd[j].zou;//printf("****%d*****\n",away);ans++;}}printf("%d\n",ans);}return 0;}/*64 81 54 72 51 36 8*/


0 0
原创粉丝点击