HDU 2037

Problem Description
“今年暑假不AC?”
“是的。”
“那你干什么呢?”
“看世界杯呀,笨蛋!”
“@#$%^&*%...”

确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。
作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)

Input
输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。

Output
对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。

Sample Input
12 1 3 3 4 0 7 3 8 15 19 15 20 10 15 8 18 6 12 5 10 4 14 2 9 0

Sample Output
5

Author
lcy

Source

ACM程序设计期末考试(2006/06/07)

题意:很显然,这是一个贪心问题,从一开始就选择结束时间最早的节目开始看,往下遍历下一个结束时间早且尚未开始的节目。

先来一个WA代码。。。我也不知道咋肥四呜呜呜呜呜5555555555.。。。//好吧,是因为这是day3的题,我已经忘了233333

#include <iostream>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;

//______________________B______________________________
struct tv{
        int beg;
        int en;
    }t[1001];
inline int cmp(const tv x,const tv y)
{
    return (x.en<y.en);
}
int main()
{
    freopen("1.in","r",stdin);
//    freopen("12.out","w",stdout);
    int n,last_end=9999;
while(scanf("%d",&n)&&n)
{
    int cnt=0;
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&t[i].beg,&t[i].en);
    }
    sort(t,t+n,cmp);
    for(int i=0;i<n;i++)
    {
        //printf("%d=%d %d\n",i+1,t[i].beg,t[i].en);
        if(i==0)
        {
            last_end=t[i].en;
            cnt++;
        //    printf("1上次结束时间为%d\n",last_end);
            continue;
        }
        if(t[i].beg>=last_end)
        {
                cnt++;
            //    printf("看这个节目%d:%d\n",i+1,cnt);
                last_end=t[i].en;
            //    printf("2上次结束时间为%d\n",last_end);
        }
    }
    printf("%d\n",cnt);
}
    return 0;
}

来一个AC代码。。。很迷幻

 1 #include <iostream>
 2 #include <map>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 //______________________B______________________________
 8 struct tv{
 9         int beg;
10         int en;
11     }t[1001];
12 inline int cmp(const tv x,const tv y)
13 {
14     return (x.en<y.en);
15 }
16 int main()
17 {
18 //    freopen("1.in","r",stdin);
19 //    freopen("12.out","w",stdout);
20     int n,last_end=9999;
21 while(scanf("%d",&n)&&n)
22 {
23     int cnt=0;
24     for(int i=0;i<n;i++)
25     {
26         scanf("%d%d",&t[i].beg,&t[i].en);
27     }
28     sort(t,t+n,cmp);
29     for(int i=0;i<n;i++)
30     {
31         //printf("%d=%d %d\n",i+1,t[i].beg,t[i].en);
32         if(i==0)
33         {
34             last_end=t[i].en;
35             cnt++;
36         //    printf("1上次结束时间为%d\n",last_end);
37             continue;
38         }
39         if(t[i].beg>=last_end)
40         {
41                 cnt++;
42             //    printf("看这个节目%d:%d\n",i+1,cnt);
43                 last_end=t[i].en;
44             //    printf("2上次结束时间为%d\n",last_end);
45         }
46     }
47     printf("%d\n",cnt);
48 }
49     return 0;
50 }

好吧,我看出来了,就是忘记注释读取文件的那一行代码。珍爱生命,手动输入?

记得刘汝佳紫书曾说,可以用宏定义啥的...忘了怎么用的,感觉有点麻烦,比较生疏,所以还是记得一下比较好。。。

猜你喜欢

转载自www.cnblogs.com/greenaway07/p/10420090.html