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. C语言单元测试

    转自http://blog.csdn.net/colin719/article/details/1420583 对于敏捷开发来说,单元测试必不可少,对于Java开发来说,JUnit非常好,对于C++开 ...

  2. qemu的device参数解释 包括socket的一些知识

    前面一片是driver,是把这个新的设备“插入到虚机中”,device 是准备驱动了.device 都是和设备配合使用的.要怎么去驱动一个设备,包括使用的驱动函数是啥,device后面的函数根据驱动的 ...

  3. [剑指Offer] 8.跳台阶

     题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. [思路]与斐波那契数列类似 class Solution { public: int jumpF ...

  4. 猜数字(C语言版)

    编程先由计算机“想”一个1到100之间的数请人猜,如果人猜对了,则结束游戏,并在屏幕上输出人猜了多少次才猜对此数,以此来反映猜数者“猜”的水平,否则计算机给出提示,告诉人所猜的数是太大还是太小,最多可 ...

  5. JavaScript实现键盘操作页面跳转

    对于使用笔记本的同学来说,鼠标操作比较费劲,键盘操作比较方便,下面是一段JavaScript写的,用键盘来实现页面跳转.把location后面的改成你要跳转的地址即可,示例是用方向键实现日志页面的前一 ...

  6. 【算法】高斯消元&线性代数

    寒假作业~就把文章和题解3道题的代码扔在这里啦——链接: https://pan.baidu.com/s/1kWkGnxd 密码: bhh9 1.HNOI2013游走 #include <bit ...

  7. [BZOJ4212]神牛的养成计划

    [BZOJ4212]神牛的养成计划 试题描述 Hzwer 成功培育出神牛细胞,可最终培育出的生物体却让他大失所望...... 后来,他从某同校女神 牛处知道,原来他培育的细胞发生了基因突变,原先决定神 ...

  8. BZOJ1559 [JSOI2009]密码 【AC自动机 + 状压dp】

    题目链接 BZOJ1559 题解 考虑到这是一个包含子串的问题,而且子串非常少,我们考虑\(AC\)自动机上的状压\(dp\) 设\(f[i][j][s]\)表示长度为\(i\)的串,匹配到了\(AC ...

  9. Android webview “location.replace” 不起作用

    js解决方法: function locationReplace(url){ if(history.replaceState){ history.replaceState(null, document ...

  10. 东北育才冲刺noip(day9)

    这十天来呢,感觉自己进步很大,(虽然被碾压的很惨),看到了自己以前完全没见过,也没想过的算法,打开新世界的大门. 同时呢,也感觉自己太弱了,于是就注册了这个博客. 为了促进进步,在这里立下flag,我 ...