hdu5887

大致题意:有一个人做事情,每件事情价值v,耗时t,在规定的时间内做总价值和最大的事情,求最大价值

测试案例:

input:

3 70(n,T)
71 100(ti,vi)
69 1
1 2



output:

3


解题思路:这题看上去是0-1背包,但是时间t太大,0-1背包不好开这么大的数组,所以用搜索写,这里开2个后缀数组,分别是到当前位置i时i-n的时间总和与到当前位置i时i-n的价值总和,用于剪枝,即当前搜索到的位置所用时间+后缀时间>T即剪枝以及当前搜索到的位置所得到价值+后缀价值<=sum即剪枝,另外时间按从大到小排序,从小到大排序会超时..

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>

using namespace std;

typedef long long ll;
const int maxn=110;

struct node
{
    ll t,v;
    bool operator<(const node &nd)const
    {
        return t>nd.t;
    }
};

node nd[maxn];

int n,cnt;
ll sum,T;
ll rv[maxn],rt[maxn];

void dfs(int i,ll uv,ll ut)
{
    sum=max(uv,sum);
    if (i==n)
    {
        return;
    }
    if (sum>=uv+rv[i])
        return;
    if (T>=ut+rt[i])
    {
        sum=max(uv+rv[i],sum);
        return;
    }
    if (ut+nd[i].t<=T)
    {
        dfs(i+1,uv+nd[i].v,ut+nd[i].t);

    }
    dfs(i+1,uv,ut);
}


int main()
{
    int i;
    while (scanf("%I64d%I64d",&n,&T)==2)
    {
        cnt=0;
        for (i=0; i<n; i++)
        {
            scanf("%I64d%I64d",&nd[cnt].t,&nd[cnt].v);
            if (nd[cnt].t<=T)
            {
                cnt++;
            }
        }
        n=cnt;
        sort(nd,nd+n);
        rv[n]=rt[n]=0;
        for (i=n-1; i>=0; i--)
        {
            rv[i]=rv[i+1]+nd[i].v;
            rt[i]=rt[i+1]+nd[i].t;
        }

        sum=0;
        dfs(0,0,0);
        printf("%I64d\n",sum);
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值