hdu 5188(带限制的01背包)
zhx and contest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 757 Accepted Submission(s): 282
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.
For each test, there are two integers n and w separated by a space. (1≤n≤30, 0≤w≤109)
Then come n lines which contain three integers ti,vi,li. (1≤ti,li≤105,1≤vi≤109)
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).
1 4 7
3 6
4 1 8
6 8 10
1 5 2
2 7
10 4 1
10 2 3
8
zhx is naive!
#include<iostream>
#include<cstdio>
#include<cstring>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int N = ;
struct Node{
int t,v,l;
}node[];
int dp[N];
int n,w;
int cmp(Node a,Node b){
if(a.l-a.t==b.l-b.t) return a.l<b.l; ///按照开始时间进行排序
return a.l-a.t<b.l-b.t;
}
int main()
{
while(scanf("%d%d",&n,&w)!=EOF){
int s = ;
for(int i=;i<=n;i++){
scanf("%d%d%d",&node[i].t,&node[i].v,&node[i].l);
s+=max(node[i].l,node[i].t);
}
sort(node+,node++n,cmp);
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++){
int start = max(node[i].l,node[i].t);
for(int j=s;j>=start;j--){
dp[j] = max(dp[j],dp[j-node[i].t]+node[i].v);
}
}
int ans = s+;
for(int i=;i<=s;i++){
if(dp[i]>=w){
ans = i;
break;
}
}
if(ans!=s+) printf("%d\n",ans);
else printf("zhx is naive!\n");
}
return ;
}
hdu 5188(带限制的01背包)的更多相关文章
- HDU 5234 Happy birthday --- 三维01背包
HDU 5234 题目大意:给定n,m,k,以及n*m(n行m列)个数,k为背包容量,从(1,1)开始只能往下走或往右走,求到达(m,n)时能获得的最大价值 解题思路:dp[i][j][k]表示在位置 ...
- HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...
- HDOJ(HDU).2602 Bone Collector (DP 01背包)
HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...
- HDU 1864 最大报销额 0-1背包
HDU 1864 最大报销额 0-1背包 题意 现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A类).文具(B类).差旅(C类),要求每张发票的总额不得超过1000元,每张发票上, ...
- HDU 2546 饭卡(带限制的01背包变形)
思路:有几个解法,如下 1)先拿出5块买最贵的菜,剩下的菜再进行01背包.如何证明正确性?设最贵的菜价e,次贵的菜价s,设减去5后的余额为x,会不会产生这样的情况,假设用5元买了e,余额最多能买到x- ...
- hdu 5188 zhx and contest [ 排序 + 背包 ]
传送门 zhx and contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- hdu 2639 第k大01背包
求每个状态里的k优解,然后合并 /* HDU 2639 求01背包的第k大解. 合并两个有序序列 */ #include<stdio.h> #include<iostream> ...
- 【HDU 3810】 Magina (01背包,优先队列优化,并查集)
Magina Problem Description Magina, also known as Anti-Mage, is a very cool hero in DotA (Defense of ...
随机推荐
- mysql初识(5)
将mysql数据库内的表导出为execel格式文件: 方法1:mysql命令:select * into outfile '/tmp/test.xls' from table_name;(需要注意的是 ...
- 【转】C++ const用法 尽可能使用const
http://www.cnblogs.com/xudong-bupt/p/3509567.html C++ const 允许指定一个语义约束,编译器会强制实施这个约束,允许程序员告诉编译器某值是保持不 ...
- vue里的this
vue中methods对象里的函数, this指向的都是当前实例或者组件.
- Java面试题(下)
这部分主要是开源Java EE框架方面的内容,包括hibernate.MyBatis.spring.Spring MVC等,由于Struts 2已经是明日黄花,在这里就不讨论Struts 2的面试题, ...
- systemtap get var of the tracepoing
kernel.trace("sched_switch") func:func:perf_trace_sched_stat_template get the function in ...
- struts2的验证
1.原理 当浏览器向服务器提交表单数据时,在服务器端需要对表单数据的有效性进行校验. “校验方法”会在“业务方法”之前调用. 2.实现验证的两种方式 struts2校验的两种实现方法: 1. 手工编写 ...
- Windows 64下elasticsearch-1.7.1集群 安装、启动、停止
elasticsearch-1.7.1 (es Windows 64) 安装.启动.停止的详细记录 https://blog.csdn.net/qq_27093465/article/details/ ...
- hdu 3231 Box Relations (拓扑排序)
Box Relations Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- WIN7服务优化,别关太多,小心启动不
原文链接地址:http://blog.csdn.net/civilman/article/details/51423972 Adaptive brightness 监视周围的光线状况来调节屏幕明暗,如 ...
- 大学本科毕业论文——LanguageTool语法校正规则库的开发
原创率超高的毕业论文,基本没有太多抄袭的东西,论述观点完全是1年半前的我的想法,或许bug很多,仅作发布参考,不作讨论. 参考预览图: 只读pdf版本下载地址: http://download.csd ...