zoukankan      html  css  js  c++  java
  • hdu 2037 贪心基础

    hdu2037今年暑假不AC 

    题目大意:n个节目,开始时间以及结束时间已知,求最多可以完整看完的节目的个数。

    思路:首先将节目按结束时间早晚排序,可以证明从第【1-n】个节目中选,最早结束的节目最后肯定在观看计划之内,从第【2-n】个节目中选,如果第二个节目不和前一个节目冲突那么肯定也在观看计划内,一次类推,遍历一次节目单。典型的贪心思想。

    code:

    #include <iostream>
    #include<cstdio>
    #include<algorithm>
    #define maxn 105
    using namespace std;
    typedef struct Pin{
        int begin;
        int end;
    }Prog;
    Prog p[maxn];
    bool compare(Prog a,Prog b)
    {
        return a.end < b.end;
    }
    int main()
    {
        int n;
        while(scanf("%d",&n)&&n!=0){
            for(int i = 0;i < n;i ++)
            {
                scanf("%d %d",&p[i].begin,&p[i].end);
            }
            sort(p,p+n,compare);
            int ans = 1,t = 0;
            for(int i = 1;i < n;i ++)
            {
                if(p[i].begin >= p[t].end){
                    ans ++;
                    t = i;
                }
            }
            printf("%d
    ",ans);
        }
    
        return 0;
    }
  • 相关阅读:
    from import 的认识
    模块初识
    eq方法
    hash介绍
    item系列
    析构函数
    serializers进阶
    APIView源码解析
    RPC协议
    面试题补充
  • 原文地址:https://www.cnblogs.com/zhangjialu2015/p/5296980.html
Copyright © 2011-2022 走看看