今年暑假不AC

hdu2037(今年暑假不AC):http://acm.hdu.edu.cn/showproblem.php?pid=2037

题意:暑期到了,同学们不再想做题了,想放松一下看电视,但是重要的是不管看什么节目,我们只想看尽可能多的节目。

给出一个n,表示节目的个数。然后n对数,表示每个节目的开始和结束时间。

题解:简单的贪心。(个人还认为可以用匹配来做)我们尽可能选择先结束的节目。利用贪心,每次都选择最优的也即是

结束时间尽可能早的。当然,有一个条件就是要选的节目起始时间一定大于等于前一个节目的结束时间。首先我们对节目

按照结束时间的由小到大排序这样就可以只在一个for循环内将其解决了。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 struct Node{
 7     int start;
 8     int end;
 9     const bool operator<(const Node &a)const{
10         return end<a.end;
11     }
12     
13 }edge[102];
14 int n,counts,e;
15 
16 int main(){
17     while(~scanf("%d",&n)&&n){
18         for(int i=1;i<=n;i++){
19             scanf("%d%d",&edge[i].start,&edge[i].end);
20         }
21         counts=0;
22         e=0;
23         sort(edge+1,edge+n+1);
24         for(int i=1;i<=n;i++){
25             if(edge[i].start>=e){
26                 e=edge[i].end;
27                 counts++;
28             }
29         }
30         printf("%d
",counts);
31     }
32 }
View Code
原文地址:https://www.cnblogs.com/chujian123/p/3356126.html