• 贪心算法


    例题一:

    题目来源:HDOJ-2187:悼念512汶川大地震遇难同胞——老人是真饿了

    题目大意:假设市场有m种大米,各种大米的单位价格和重量已知。为了满足很多其它灾民的需求。问最多能採购多少重量的大米。

    题目分析:对价格的进行排序。用最少的钱买很多其它的大米。

    AC代码:

     

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    struct price		//定义一个结构体 
    {
        int p;		//p代表每种大米单位价格 
        int h;		//h代表每种大米重量 
    }str[1010];
    bool cmp(struct price x,struct price y)		//依照单位价格进行排序 
    {
        return x.p<y.p;
    }
    int main()
    {
        int t,n,m,i;
        double s,s2;
        scanf("%d",&t);
        while(t--)
        {
            s2=0;		//s2代表可以买的大米的总重 
            scanf("%d%d",&n,&m);
            for(i=0;i<m;i++)
                scanf("%d%d",&str[i].p,&str[i].h);
            sort(str,str+m,cmp);		//快排 
            for(i=0;i<m;i++)
            {
                if(i)
                    n-=str[i-1].p*str[i-1].h;
                s=(double)n/str[i].p;		//s代表每种大米的重量 
                if(s<str[i].h)
                {
                    s2+=s;
                    break;
                }
                else
                    s=str[i].h;
                s2+=s;
            }
            printf("%.2lf
    ",s2);
        }
        return 0;
    }

    例题二:

    题目来源:HDOJ-2037:今年暑假不AC

    题目大意:如果你已经知道了全部你喜欢看的电视节目的转播时间表,要求你进行合理安排使得你可以看尽量多的完整节目。

    题目分析:首先应该将喜欢的节目的转播时间进行排序,然后再依次选择要看的完整节目。

    AC代码:

     

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    struct str		//定义一个结构体 
    {
        int start;
        int end;
    }a[110];
    bool cmp(struct str x,struct str y)		//依照节目的结束、開始时间排序 
    {
        if(x.end==y.end)
            return x.start>y.start;		//開始时间降序 
        else
            return x.end<y.end;		//结束时间升序 
    }
    int main()
    {
        int n,i,e,num;
        while(scanf("%d",&n)&&n)
        {
            num=1;
            for(i=0;i<n;i++)
                scanf("%d%d",&a[i].start,&a[i].end);
            sort(a,a+n,cmp);		//快排 
            e=a[0].end;
            for(i=1;i<n;i++)
                if(e<=a[i].start)		//推断能否够继续下一个节目 
                {
                    e=a[i].end;
                    num++;
                }
            printf("%d
    ",num);
        }
        return 0;
    }

    例题三:

    题目来源:HDOJ-1789:Doing Homework again

    题目大意:立即就要放假了,各科老师都留了作业,要求在指定的时间完毕,假设完不成就将扣除对应的分数。每天仅仅能完毕一科作业。问最少会扣除多少分数。

    题目分析:对于这种问题肯定是先把分数高的先做完,才干做到扣除的分数最少。

    AC代码:

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    int b[1010];
    struct limit		//定义结构体 
    {
        int day;
        int score;
    }a[1010];
    bool cmp(struct limit x,struct limit y)		//首先按分的大小排序。其次按天数的少多排序 
    {
        if(x.score==y.score)
            return x.day<y.day;
        else
            return x.score>y.score;
    }
    int main()
    {
        int t,n,i,j,sum,ans;
        scanf("%d",&t);
        while(t--)
        {
            sum=0;
            memset(b,0,sizeof(b));		//对数组b进行初始化 
            scanf("%d",&n);
            for(i=0;i<n;i++)
                scanf("%d",&a[i].day);
            for(i=0;i<n;i++)
                scanf("%d",&a[i].score);
            sort(a,a+n,cmp);		//快排 
            for(i=0;i<n;i++)
            {
                for(j=a[i].day,ans=1;j>0;j--)
                {
                    if(b[j]==0)		//推断这天是否已经做过作业 
                    {
                        b[j]=1;
                        ans=0;
                        break;
                    }
                }
                if(ans)		//推断这份作业是否可以完毕 
                    sum+=a[i].score;
            }
            printf("%d
    ",sum);
        }
        return 0;
    }


  • 相关阅读:
    MySQL用户信息表中主键userID自动增加问题
    PHP输出当前系统时间
    PHP连接MySQL方式比较问题
    DWZ分页处理
    NHibernate ICriteria 查询[转 十年如一]
    HttpContext.Current.Request.Files后台取不到值的解决方法 [转]
    Hibernate Projections(投影、统计、不重复结果)[转]
    细说Form(表单)[ 转 Fish Li]
    HTML <a> 标签的 rel 属性
    dwz rel
  • 原文地址:https://www.cnblogs.com/jhcelue/p/7345845.html
Copyright © 2020-2023  润新知