hdu5188 加限制的01背包问题「建议收藏」

hdu5188 加限制的01背包问题

大家好,又见面了,我是全栈君。

http://acm.hdu.edu.cn/showproblem.php?

pid=5188

Problem Description
As one of the most powerful brushes in the world, zhx usually takes part in all kinds of contests.

One day, zhx takes part in an contest. He found the contest very easy for him.

There are 



n
 problems in the contest. He knows that he can solve the 



ith
 problem in 



ti
 units of time and he can get 



vi
 points.

As he is too powerful, the administrator is watching him. If he finishes the 



ith
 problem before time 



li
, he will be considered to cheat.

zhx doesn’t really want to solve all these boring problems. He only wants to get no less than 



w
 points. You are supposed to tell him the minimal time he needs to spend while not being considered to cheat, or he is not able to get enough points. 

Note that zhx can solve only one problem at the same time. And if he starts, he would keep working on it until it is solved. And then he submits his code in no time.
 


Input
Multiply test cases(less than 



50
). Seek 



EOF
 as the end of the file.

For each test, there are two integers 



n
 and 



w
 separated by a space. (



1n30




0w109
)

Then come n lines which contain three integers 



ti,vi,li
. (



1ti,li105,1vi109
)
 


Output
For each test case, output a single line indicating the answer. If zhx is able to get enough points, output the minimal time it takes. Otherwise, output a single line saying “zhx is naive!” (Do not output quotation marks).
 


Sample Input
   
   
1 3 1 4 7 3 6 4 1 8 6 8 10 1 5 2 2 7 10 4 1 10 2 3

 


Sample Output
   
   
7 8 zhx is naive!

/**
hdu5188 有限制条件的01背包问题
题目大意:有n道题i题用时ti秒,得分vi,在li时间点之前不能做出来,并且一道题不能分开几次做(一旦開始做,必须在ti时间内把它做完)
          问得到w分的最小用时是多少
解题思路:非常像01背包的基本题,可是有一个li分钟前不能AC的限制,因此第i道题必须在最早第(li-ti)时刻做,我们依照l-t递增排序,然后依照经典解法来做
          即可了
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;

struct note
{
    int t,v,l;
    bool operator <(const note &other)const
    {
        return l-t<other.l-other.t;
    }

}node[35];

int n,m;
int dp[3000005];

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int sum=0,ans=0,up=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&node[i].t,&node[i].v,&node[i].l);
            sum+=node[i].v;
            ans+=node[i].t;
            up=max(up,node[i].l);
        }
        if(m>sum)
        {
            printf("zhx is naive!\n");
            continue;
        }
        sort(node,node+n);
        up=max(up,ans);
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n;i++)
        {
            for(int j=up;j>=node[i].l;j--)
            {
                if(j>=node[i].t)
                {
                    dp[j]=max(dp[j],dp[j-node[i].t]+node[i].v);
                }
            }
        }
        int flag=0;
        for(int i=0;i<=up;i++)
        {
            if(dp[i]>=m)
            {
                printf("%d\n",i);
                flag=1;
                break;
            }
        }
        if(flag==0)
        {
            printf("zhx is naive!\n");
        }
    }
    return 0;
}

Problem Description
As one of the most powerful brushes in the world, zhx usually takes part in all kinds of contests.

One day, zhx takes part in an contest. He found the contest very easy for him.

There are 



n
 problems in the contest. He knows that he can solve the 



ith
 problem in 



ti
 units of time and he can get 



vi
 points.

As he is too powerful, the administrator is watching him. If he finishes the 



ith
 problem before time 



li
, he will be considered to cheat.

zhx doesn’t really want to solve all these boring problems. He only wants to get no less than 



w
 points. You are supposed to tell him the minimal time he needs to spend while not being considered to cheat, or he is not able to get enough points. 

Note that zhx can solve only one problem at the same time. And if he starts, he would keep working on it until it is solved. And then he submits his code in no time.
 


Input
Multiply test cases(less than 



50
). Seek 



EOF
 as the end of the file.

For each test, there are two integers 



n
 and 



w
 separated by a space. (



1n30




0w109
)

Then come n lines which contain three integers 



ti,vi,li
. (



1ti,li105,1vi109
)
 


Output
For each test case, output a single line indicating the answer. If zhx is able to get enough points, output the minimal time it takes. Otherwise, output a single line saying “zhx is naive!” (Do not output quotation marks).
 


Sample Input
    
    
1 3 1 4 7 3 6 4 1 8 6 8 10 1 5 2 2 7 10 4 1 10 2 3

 


Sample Output
    
    
7 8 zhx is naive!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116013.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Android之使用weight属性实现控件的按比例分配空间

    从今天开始,把看书时候的知识点整理成博客,这个比较简单,估计有经验的都用过,weight属性 在做Android布局的时候,经常遇到需要几个控件按比例分配空间的情况比如下图效果在底部设置两个button,占据底部宽度一部分的同时,保持1:3的比例,当然了,这么难看的布局用处不大,仅是用来说明weight的用法布局代码如下:

    2022年3月11日
    43
  • 信号处理中包络是什么意思_重庆邮电大学复试通信原理

    信号处理中包络是什么意思_重庆邮电大学复试通信原理第一章绪论1.基带信号的定义基带信号是指信号的频谱从零频附近开始的,没有经过调制的信号2.什么是数字信号和模拟信号?二者的区别是什么?数字信号是信号参量的取值是离散的,模拟信号是信号参量的取值是连续的。区别是信号参量的取值是连续还是离散。3.什么是数字通信?描述数字通信系统的主要优缺点?数字通信就是用数字信号传输信息的通信系统。数字通信系统的优点有差错可控,抗干扰能力强,易于存储,处理和…

    2022年8月10日
    6
  • Cinemachine(三)自动选择/切换最适合的摄像头(Cinemachine Clear Shot Camera)「建议收藏」

    Cinemachine(三)自动选择/切换最适合的摄像头(Cinemachine Clear Shot Camera)「建议收藏」在很多的解谜类游戏中,

    2022年5月8日
    51
  • feign原理详解_vip视频解析是什么原理

    feign原理详解_vip视频解析是什么原理Feign原理解析基本原理现在已经了解了Ribbon的负载均衡原理,我们可以来猜想下,Feign的原理,仅仅通过一个注解@FeignClient+一个接口,就可以服务之间的调用。通过@FeignClient在注解中的name,确定服务名,然后RibbonClient使用服务名去获取负载均衡器loadBalancer,再通过负载均衡算法获取到ip:port,然后构建的请求为http://nacos-component-provider/test/{id},当id=1

    2022年10月4日
    0
  • java详细安装教程(供新手参考)一一java(jdk)安装

    java详细安装教程(供新手参考)一一java(jdk)安装一、java历史简介1991年Sun公司的JamesGosling等人开始开发名称为Oak(橡树)的语言。希望用于控制嵌入在有线电视交换盒、PDA等的微处理器,1994年将Oak语言更名为Java1998年JDK1.2时,更名为Java2Platform分为标准版J2SE,企业版J2EE,微型版J2MEJava既安全、可移植,又可跨平台,而且人们发现它能够解决In…

    2022年7月8日
    25
  • excel 汉字转拼音「建议收藏」

    excel 汉字转拼音「建议收藏」Functionpinyin(pAsString)AsString’*************************************’版本说明:转载请保留此段注释’更新时间:2018年8月28日’作者:上海五航航空技术有限公司李晓锋’感谢:“在线汉语字典”的中文转拼音功能http://xh.5156edu.com/conversion.html,大大的加快了拼音的转换速度。’说明:本代码几乎包含了Excel表中能够出现的所有汉字(20830个汉字),去除了无法使用“在线汉语

    2022年6月21日
    27

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号