hdu 5188 zhx and contest(有限制的01背包问题)

zhx and contest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 755    Accepted Submission(s): 281


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. ( 1n30 0w109 )
Then come n lines which contain three integers  ti,vi,li . ( 1ti,li105,1vi109 )
 

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
 

题意:有n个任务,每个任务有所需时间,价值跟最早完成时间

现在问你至少得到价值w需要最少多长时间

思路:乍看就是背包问题,可是跟普通的背包又不太一样。

这个题是要求>=价值的最小时间

跟我们一般背包的<=要求的最大价值不太一样

可是我们依然可以用背包来解,我们把每个任务看成是一件物品。

物品有消耗(时间),价值。然后起码从最早完成时间开始才能开始取这个物品

那么我们所要的结果就是最后枚举时间,第一个>=w的时间,如果不行则输出zhx is naive!(+1s?)

那么此时问题来了,时间的上限是多少?  一开始我以为30*100000的背包会超时。。结果居然没超

大概是数据水了。  我们的背包上限当然也不能设置为3000000,我们可以设为max(所有任务所需时间,所有任务最早完成时间里最大的一个)

还要注意排序的问题,我们知道一般的01背包是不需要排序的,但是这个题是需要的,因为这个题可能会有等待时间

也就是某个任务可能为了完成,需要等待几秒钟。但是此时如果先做另一个任务,这个任务就不需要等待了,就能在更短的时间内得到更大的价值。

所以我们按照每个任务的开始时间排序,我们知道,如果我们在做这个任务的时候,所有能开始的任务都已经更新过了,那么此时的结果肯定是最优的了。

而按照结束时间排序无法做到这一点。

这样就能500ms水过去了

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 50
struct Node
{
    int t,v,l;
} a[N];
bool cmp(Node a,Node b)
{
    return a.l-a.t<b.l-b.t;
}
long long dp[3000002];
int solve(int sum,int w)
{
    for(int i=0; i<=sum; i++)
        if(dp[i]>=w)
            return i;
    return -1;
}
int main()
{
    int n,w;
    int t,v,l;
    while(~scanf("%d %d",&n,&w))
    {
        memset(dp,0,sizeof(dp));
        int sum=0,sum1=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d %d %d",&a[i].t,&a[i].v,&a[i].l);
            sum+=a[i].t;
            sum1=max(sum1,a[i].l);
        }
        sort(a+1,a+1+n,cmp);
        sum=max(sum,sum1);
        for(int i=1; i<=n; i++)
            for(int j=sum; j>=max(a[i].t,a[i].l); j--)
                dp[j]=max(dp[j],dp[j-a[i].t]+a[i].v);
        int ans=solve(sum,w);
        if(ans==-1) printf("zhx is naive!\n");
        else printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值