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. lintcode-51-上一个排列

    51-上一个排列 给定一个整数数组来表示排列,找出其上一个排列. 注意事项 排列中可能包含重复的整数 样例 给出排列[1,3,2,3],其上一个排列是[1,2,3,3] 给出排列[1,2,3,4],其 ...

  2. 【SSH】——Hibernate实现简单的自动建表

    [与ORM] Object Relational Mapping,对象关系映射,将对象和关系联系了起来.面向对象是从耦合.聚合.封装等的基础上发展起来的,而关系数据库则是从数学理论发展而来的,两套理论 ...

  3. 在程序内部跳转到下一个页面 和 向另一个servlet发起跳转

    request.getRequestDispatcher("/success.html").forward(request,response); request.getReques ...

  4. 【题解】CQOI2015选数

    这题做的时候接连想错了好多次……但是回到正轨上之后依然是一个套路题.(不过这题好像有比莫比乌斯反演更好的做法,莫比乌斯反演貌似是某种能过的暴力ヽ(´ー`)┌)不过能过也就行了吧哈哈. 首先我们把数字的 ...

  5. 【题解】POI2014FAR-FarmCraft

    这题首先手玩一下一下数据,写出每个节点修建软件所需要的时间和到达它的时间戳(第一次到达它的时间),不难发现实际上就是要最小化这两者之和.然后就想到:一棵子树内,时间戳必然是连续的一段区间,而如果将访问 ...

  6. 【BZOJ 1930】 [Shoi2003]pacman 吃豆豆 最大费用最大流

    如果你知道他是网络流的话你就很快会想到一个最大费用最大流的模型,然后你发现可能T,然而你发现你只用增广两次,然后你就开心的打了出来,然后发现被稠密图里spfa的丧病时间复杂度坑了,还是会T.于是我就开 ...

  7. synchronized ---- 作用

    获得同步锁: 1.清空工作内存: 2.从主内存拷贝对象副本到工作内存: 3.执行代码(计算或者输出等): 4.刷新主内存数据: 5.释放同步锁.

  8. org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.oskyhang.gbd.service.UserService] found for dependency: expected at least 1 bean which qualifies as aut

    spring中一个符号的错误居然让我浪费了四五个小时才找出来,不得不给自己了两个耳光.. 由于新建项目与原来项目的目录结构有所不同,copy过来的配置文件,有些地方修改的不彻底,导致spring扫描注 ...

  9. 通过AWS的DHCP自动获取的IP地址是否会发生改变?

    针对您的问题,分析如下:1.在一个VPC内,通过AWS的DHCP自动获取的IP地址,在如何情况下会发生改变?例如我把vpc的内所有100个ec2实例全部关闭,再全部重新打开,是否会发生IP地址变化的情 ...

  10. Extend the size of ext3 partition online in VM

    1. Increase disk space in vCenter 2. scan disk on the Linux VM # fdisk -lu > /tmp/fdisk. # > / ...