Functioning of a scientific conference is usually divided into several simultaneous sections. For example, there may be a section on parallel computing, a section on visualization, a section on data compression, and so on.
Obviously, simultaneous work of several sections is necessary in order to reduce the time for scientific program of the conference and to have more time for the banquet, tea-drinking, and informal discussions. However, it is possible that interesting reports are given simultaneously at different sections.
A participant has written out the time-table of all the reports which are interesting for him. He asks you to determine the maximal number of reports he will be able to attend.
Input
The first line contains the number 1 ≤ N ≤ 100000 of interesting reports. Each of the next N lines contains two integers Ts and Te separated with a space (1 ≤ Ts < Te ≤ 30000). These numbers are the times a corresponding report starts and ends. Time is measured in minutes from the beginning of the conference.
Output
You should output the maximal number of reports which the participant can attend. The participant can attend no two reports simultaneously and any two reports he attends must be separated by at least one minute. For example, if a report ends at 15, the next report which can be attended must begin at 16 or later.
Sample
input | output |
5
3 4
1 5
6 7
4 5
1 3
|
3
|
中文翻译
科学讨论会议常常可分为几个不同的部分。比如,有的是讨论计算的,有的是讨论数据压缩的等等。
为了节省时间,几个不同的部分往往同时举行,以便有更多时间举行宴会,喝茶,非正式讨论。并且,不同的部分都可能有精彩的报告会。
给出所有报告会的时间安排表,要求你求出最多能够参加多少场报告会。
Input
第一行为报告会的总数N, 1 <= N <= 100000。接下来有N行,每一行有两个整数 T_s 和 T_e ,一空格隔开 (1 <= T_s < T_e <= 30000),报告会的开始时间和结束时间,单位为分钟。
Output
你应该输出能够参加的报告会的最大数目。不能参加同时举行的报告会,并且两场报告会之间至少要有一分钟的时间差。比如,如果一场报告会的结束时间在15,那么能够参加的下一场报告会的开始时间应该在16或更后的时间。
---------华丽的分割线---------
分析:这道题是在讲贪心的时候做的练习,就直接用贪心写了。
这是个普通的活动选择问题,按结束时间升序排个序,再去比较开始时间就行了。
下面是代码,排序直接调用的快排
1 #include <stdio.h>
2 #include <algorithm>
3
4 using namespace std;
5
6 struct hui{
7 int s, e;
8 }wuli[ 100086 ];
9
10 bool cmp( hui a, hui b){
11 return a.e < b.e;
12 }
13
14 int main()
15 {
16 int n, t;
17
18 scanf("%d", &n);
19 int i;
20 for( i = 1; i <= n; i++ ){
21 scanf("%d%d", &wuli[ i ].s, &wuli[ i ].e);
22 }
23
24 sort( wuli + 1, wuli + 1 + n, cmp);
25
26 int et = wuli[ 1 ].e;
27 int counter = 1;
28 for( i = 2; i <= n; i++){
29 if( wuli[ i ].s > et ){
30 et = wuli[ i ].e;
31 counter++;
32 }
33 }
34
35 printf("%d\n", counter);
36 }