hdu5188(01背包变形)
zhx and contest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 725 Accepted Submission(s): 266
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 aren 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 theith 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 thanw 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.
One day, zhx takes part in an contest. He found the contest very easy for him.
There are
As he is too powerful, the administrator is watching him. If he finishes the
zhx doesn't really want to solve all these boring problems. He only wants to get no less than
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 integersn and w separated
by a space. (1≤n≤30 , 0≤w≤109 )
Then come n lines which contain three integersti,vi,li .
(1≤ti,li≤105,1≤vi≤109 )
For each test, there are two integers
Then come n lines which contain three integers
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!
题意:
有n道题目,每道题有一个得分v和用时t;
我们要得够w分;用时最少
每道题有一个开始时间,也就是开始做该题的时间不得少于一个时间l求得到w分的最短时间
思路:
如果没有l的限制,很明显的01背包,现在有了l的限制,所以要先排序,每个题最快完成的时间等于l-t,按照这个一排序就ok,然后用01背包来对时间dp,表示时间i能取得的最大分数。
#include <iostream> #include <stdio.h> #include <stdlib.h> #include<string.h> #include<algorithm> #include<math.h> #include<queue> #include<stack> using namespace std; typedef long long ll; struct data { int cost,val,l; } a[50]; int dp[100010]; bool cmp(data a,data b) { if(a.l-a.cost!=b.l-b.cost) return a.l-a.cost<b.l-b.cost; return a.val>b.val; } int main() { int n,w; while(~scanf("%d%d",&n,&w)) { int l=0,sum=0; for(int i=0; i<n; i++) scanf("%d%d%d",&a[i].cost,&a[i].val,&a[i].l),l=max(l,a[i].l),sum+=a[i].val; if(sum<w) { puts("zhx is naive!"); continue; } sort(a,a+n,cmp); memset(dp,0,sizeof(dp)); for(int i=0; i<n; i++) for(int j=l; j>=a[i].l; j--) if(j>=a[i].cost) dp[j]=max(dp[j],dp[j-a[i].cost]+a[i].val); int flag=0; for(int i=0; i<=l; i++) if(dp[i]>=w) { flag=1; printf("%d\n",i); break; } if(!flag)puts("zhx is naive!"); } return 0; }
持续更新博客地址:
blog.csdn.net/martinue