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

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. (1≤n≤30, 0≤w≤109)
Then come n lines which contain three integers ti,vi,li. (1≤ti,li≤105,1≤vi≤109)
 
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!
 
Source
 
 
题意:zhx要完成 n 道题目,完成第i道题目所需时间为 ti ,它必须在不小于 li 的时间内完成,获得的分数是 vi,现在要求zhx获得 w 分,问他最少需要多少时间来获得 w 分?
题解:如果没有 li 的限制就是一道 01 背包。。加了限制就变成难题了。。。我们应该按照开始时间 l - t 进行排序,为什么?l-t是开始时间 ,我们假设start1 < start2 ,那么从1开始做这件两件事情是要小于从2开始做这两件事情的。所以排完序之后再做01背包得到最少的时间。
#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背包)的更多相关文章

  1. HDU 5234 Happy birthday --- 三维01背包

    HDU 5234 题目大意:给定n,m,k,以及n*m(n行m列)个数,k为背包容量,从(1,1)开始只能往下走或往右走,求到达(m,n)时能获得的最大价值 解题思路:dp[i][j][k]表示在位置 ...

  2. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

  3. HDOJ(HDU).2546 饭卡(DP 01背包)

    HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...

  4. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  5. HDU 1864 最大报销额 0-1背包

    HDU 1864 最大报销额 0-1背包 题意 现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A类).文具(B类).差旅(C类),要求每张发票的总额不得超过1000元,每张发票上, ...

  6. HDU 2546 饭卡(带限制的01背包变形)

    思路:有几个解法,如下 1)先拿出5块买最贵的菜,剩下的菜再进行01背包.如何证明正确性?设最贵的菜价e,次贵的菜价s,设减去5后的余额为x,会不会产生这样的情况,假设用5元买了e,余额最多能买到x- ...

  7. hdu 5188 zhx and contest [ 排序 + 背包 ]

    传送门 zhx and contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  8. hdu 2639 第k大01背包

    求每个状态里的k优解,然后合并 /* HDU 2639 求01背包的第k大解. 合并两个有序序列 */ #include<stdio.h> #include<iostream> ...

  9. 【HDU 3810】 Magina (01背包,优先队列优化,并查集)

    Magina Problem Description Magina, also known as Anti-Mage, is a very cool hero in DotA (Defense of ...

随机推荐

  1. 解决ecplise安装mybatipse插件时报找不到jar包的错

    在安装mybatipse插件的时候一直报这个错,脑袋疼,在网上搜了半天也没有结果,最后摸索了半天解决了,这里先贴一张图 1.先找到eclipse的安装目录,然后把相应的jar包拷到plugins里去, ...

  2. lincode-58-四数之和

    58-四数之和 给一个包含n个数的整数数组S,在S中找到所有使得和为给定整数target的四元组(a, b, c, d). 注意事项 四元组(a, b, c, d)中,需要满足a <= b &l ...

  3. lintcode-74-第一个错误的代码版本

    74-第一个错误的代码版本 代码库的版本号是从 1 到 n 的整数.某一天,有人提交了错误版本的代码,因此造成自身及之后版本的代码在单元测试中均出错.请找出第一个错误的版本号. 你可以通过 isBad ...

  4. PokeCats开发者日志(七)

      现在是PokeCats游戏开发的第十二天的晚上,很不幸提交到的三个平台(360开放平台,腾讯开放平台,华为应用市场)都没通过,著作权申请也被打回来了.   心中一万只草泥马在奔腾.   得了,看来 ...

  5. HDU 2135 Rolling table

    http://acm.hdu.edu.cn/showproblem.php?pid=2135 Problem Description After the 32nd ACM/ICPC regional ...

  6. angular强制刷新

    有时候请求完毕,某些变量重新赋值后不会体现在页面上,此时需要强制刷新 $scope.$apply(function () { $scope.message ="Timeout called! ...

  7. Struts1之logic标签

    logic是Struts1中的逻辑标签 <%@ taglib prefix="logic" uri="http://struts.apache.org/tags-l ...

  8. Spark程序

    Spark认识&环境搭建&运行第一个Spark程序 2017-07-09 17:17 by 牛仔裤的夏天, 181 阅读, 0 评论, 收藏, 编辑 摘要:Spark作为新一代大数据计 ...

  9. Luogu3953 NOIP2017逛公园(最短路+拓扑排序+动态规划)

    跑一遍dij根据最短路DAG进行拓扑排序,按拓扑序dp即可.wa了三发感觉非常凉. #include<iostream> #include<cstdio> #include&l ...

  10. [CF409F]000001

    题目大意:输入一个数,输出一个数(愚人节系列) 题解:$OEIS$的$A000001$(原来我不想写的,但是洛谷的智能推荐推荐我做这个...是不是我太菜了) 卡点:无 C++ Code: #inclu ...