zhx and contest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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
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!
解题思路:
最开始我的想法是按照l排序,然后就是简单的01背包,但WA。。。
这里需要按照l-t的大小进行排序才行。因为对于时间最少的方法,一定按照题目最早可以开始做的时间的顺序做,即按照l-t的从小到大顺序来做。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 35;
struct Node
{
int t,v,l;
bool operator < (const Node prbm) const
{
return l - t < prbm.l - prbm.t;
}
}p[maxn];
int n,w;
__int64 dp[3000005];
int main()
{
while(scanf("%d%d",&n,&w)!=EOF)
{
int m = 0;
memset(dp,0,sizeof(dp));
for(int i = 1; i <= n; i++)
{
scanf("%d%d%d",&p[i].t,&p[i].v,&p[i].l);
m += p[i].t;
}
sort(p+1,p+1+n);
m += p[n].l;
for(int i = 1; i <= n; i++)
for(int j = m; j >= 0; j--)
{
if(j < p[i].l) break;
if(j >= p[i].t)
dp[j] = max(dp[j],dp[j-p[i].t] + p[i].v);
}
int ans = -1;
for(int j = 0; j <= m; j++)
if(dp[j] >= w)
{
ans = j;
break;
}
if(ans == -1)
printf("zhx is naive!\n");
else printf("%d\n",ans);
}
return 0;
}