hdu 2037 今年暑假不AC

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
",count);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/yaoyueduzhen/p/4336153.html