zoukankan      html  css  js  c++  java
  • HDU 5188 背包

    有N道题。要求得到最少W分

    给出N道题的:每道题用时T。分数V,应在且必须在L时刻提交才干得分

    问得到W分所用的最少的时间


    以L-T排序,然后做01背包就可以


    #include "stdio.h"
    #include "algorithm"
    #include "string.h"
    using namespace std;
    
    struct Mark
    {
        int t,v,l,x;
    }mark[40];
    
    int dp[300010];
    
    bool cmp(Mark a,Mark b)
    {
       if (a.x!=b.x) return a.x<b.x;
         return a.l<b.l;
    }
    int Max(int a,int b)
    {
        if (a<b) return b;
        else return a;
    }
    int main()
    {
        int n,m,i,j,ans,up,sum,sum1;
        while (scanf("%d%d",&n,&m)!=EOF)
        {
            sum=sum1=up=0;
            for (i=1;i<=n;i++)
            {
    
                scanf("%d%d%d",&mark[i].t,&mark[i].v,&mark[i].l);
                sum+=mark[i].v;
                sum1+=mark[i].t;
                up=Max(up,mark[i].l);
                mark[i].x=mark[i].l-mark[i].t;
            }
            if (sum<m) {printf("zhx is naive!
    "); continue;}
            up=Max(up,sum1);
            sort(mark+1,mark+n+1,cmp);
            memset(dp,0,sizeof(dp));
            for (i=1;i<=n;i++)
                for (j=up;j>=mark[i].l ;j--)
                dp[j]=Max(dp[j],dp[j-mark[i].t]+mark[i].v);
    
            ans=up+1;
            for (i=0;i<=up;i++)
            if (dp[i]>=m) {ans=i;break;}
            if(ans==up+1) printf("zhx is naive!
    ");
            else printf("%d
    ",ans);
        }
        return 0;
    }
    



  • 相关阅读:
    用sed删除空行
    烂泥:php5.6源码安装及php-fpm配置
    linux系统vsftpd登陆慢卡怎么办
    Linux Vsftpd 连接超时解决方法
    linux中shell截取字符串方法总结
    运算符
    数据类型
    is null 和=null的区别
    DML
    DDL
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6889842.html
Copyright © 2011-2022 走看看