HDU-#2037 今年暑假不AC(贪心)

       题目大意:给出N个节目的播放时间和结束时间,求最多能看多少节目?

       解题思路:贪心求解,将输入的节目按照结束时间排序,每次遍历获取尽可能多的节目数,详见code。

      题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=2037

      code:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

const int MAXN = 100+10;
int n,m;
struct node{
    int s,e;
}p[MAXN];

int cmp(const node a,const node b){return a.e<b.e;}

int main(){
    while(scanf("%d",&n)!=EOF && n){
        for(int i=0;i<n;i++)
            scanf("%d%d",&p[i].s,&p[i].e);
        sort(p,p+n,cmp); //将结束点作为排序基点
        int tmp=0;m=1;
        for(int i=1;i<n;i++) //贪心求解,尽量获取最多的结束点
            if(p[i].s>=p[tmp].e){
                tmp=i;
                m++;
            }
        printf("%d\n",m);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值