hdu 5188 zhx and contest ( 有限制的0/1背包)

zhx and contest

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


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
BestCoder Round #33
题目大意:有各种题,每格题有最早做完的时间,做题时间和分数,问获取一定分数的最少时间
题目分析:很明显是背包问题,但有限制条件,也就是放这个物品的时间不能早于l-t,只要超过这个时间,这个物品就可以随便放了,所以背包是不必须装满的,那么初值就是所有的都赋为0,然后我们把浪费的时间可以平移到最开始形成一段连续的容量,所以我们按照l-t排序,然后最早可选时间早的先选,然后最后枚举每个时间获得的最大收益即可,找到下标最小的不下于w的
dp[j]:j代表容量,想通了就是最简单的0/1背包,算法很水,重点是数据量,可以维护最大值,或者用map实现离散化来进行时间优化
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX 3000007

using namespace std;

int dp[MAX];
int n,w;
int up,sum,sum2;

struct Node
{
    int t,v,l;
    bool operator < ( const Node& a ) const
    {
        return l-t < a.l - a.t;
    }
}p[37];

int main ( )
{
    while ( ~scanf ( "%d%d" , &n , &w ) )
    {
        up = sum = sum2 = 0;
        for ( int i = 1 ; i <= n ; i++ )
        {
            scanf ( "%d%d%d" , &p[i].t , &p[i].v , &p[i].l );
            sum += p[i].v;
            sum2 += p[i].t;
            up = max ( up , p[i].l );
        }
        bool flag = true;
        sort ( p+1 , p+n+1 );      
        if ( sum < w ) flag = false;
        up = max ( up , sum2 );
       // cout << up << " " << sum << endl;

        memset ( dp , 0 , sizeof ( dp ) );
        for ( int i = 1 ; i <= n && flag ; i++ )
            for ( int j = up ; j >= p[i].l ; j-- )
                if ( j >= p[i].t )
                    dp[j] = max ( dp[j] , dp[j-p[i].t] + p[i].v );
        
        for ( int i = 0 ; i <= up && flag ; i++ )
            if ( dp[i] >= w ) 
            {
                printf ( "%d\n" , i );
                break;
            }
        if ( !flag ) puts ( "zhx is naive!" );
    }
}


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值