hdu 2037 今年暑假不AC

http://acm.hdu.edu.cn/showproblem.php?pid=2037

贪心的题,按结束时间来贪心,结束时间尽可能提前,看得节目就尽可能多了。

注意审题,题目说不止一组数据就得写好循环 while(scanf("%d",&n)!=EOF)等价于while(~scanf("%d",&n))也等价于while(cin>>n)

给变量赋值的位置要注意了,别再犯低级错误了....

刚开始还是没有经验,以后这类的题就知道该如何去想了。

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #define MAX 10000
 4 using namespace std;
 5 struct Node
 6 {
 7     int start,end;
 8 };
 9 bool comp(struct Node a,struct Node b)
10 {
11     if(a.end<b.end)
12       return true;
13     else
14       return false;  
15 }
16 int main()
17 {
18     int n,i,sum,t;
19     struct Node a[MAX];
20     while(~scanf("%d",&n)&&n)
21     {for(i=0;i<n;i++)
22       scanf("%d%d",&a[i].start,&a[i].end);
23     sort(a,a+n,comp);  
24     sum=0;t=0;
25     for(i=0;i<n;i++)
26     {
27         if(t<=a[i].start)
28         {sum++;t=a[i].end;}
29     }
30     printf("%d
",sum);
31     }
32     
33 }

sort()的comp函数这样写比较好理解,这样是按升序来排序,反过来就是降序了。

原文地址:https://www.cnblogs.com/xurenwen/p/3866078.html