首页 > 其他 > 详细

hdu 2037 今年暑假不AC

时间:2015-03-14 12:24:20      阅读:271      评论:0      收藏:0      [点我收藏+]

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

这道题乍看上去不知道从何处入手,其实只要将节目的时间从前到后排个序就可以了,注意是按结束时间的先后排序,排序方法和上次一样,建立一个结构体用sort函数排序。然后暴力求解:假设前一个节目要看完,记录下结束时间end,考虑后面一个节目的开始时间是否比end晚,是的话将这个节目的结束时间赋值给end,表示这个节目也要看完;不行的话考虑下一个节目,循环下去,过程中要有一个计数器,记录已经看完的节目个数。

代码如下:

#include<stdio.h>
#include<algorithm>

using namespace std;

struct node 
{
    int start;
    int end;
}time[100];

bool cmp(struct node x,struct node y)
{
    return x.end<y.end;
}

int main()
{
    int n;

    while(scanf("%d",&n)!=EOF&&n)
    {
        for(int i=0;i<n;i++)
            scanf("%d%d",&time[i].start,&time[i].end);
        sort(time,time+n,cmp);
        int count=1;
        int end=time[0].end;
        for(int i=1;i<n;i++)
        {
            if(time[i].start>=end)
            {
                count++;
                end=time[i].end;
            }
        }
        printf("%d\n",count);
    }
    return 0;
}

 

hdu 2037 今年暑假不AC

原文:http://www.cnblogs.com/yaoyueduzhen/p/4336153.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!