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*5
using 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;
}
/*
6
4 8
1 5
4 7
2 5
1 3
6 8
*/