[BZOJ4247] 挂饰

传送门

http://www.lydsy.com/JudgeOnline/problem.php?id=4247

题目大意

给定 n 个物品,每个物品都消耗1体积,但会增加ai的空间和 bi 的收益,初始有1体积的空间,求最大收益

题解

dp[i,j]=max{dp[i1,j],dp[i1,ja[i]+1]+b[i]}
注意处置

const
    maxn=2005;
var
    dp:array[0..maxn,0..2*maxn]of longint;
    x,y:array[0..maxn]of longint;
    i,j,k:longint;
    n,m,ans:longint;
function max(a,b:longint):longint;
begin if a>b then exit(a) else exit(b); end;

procedure sort(l,r:longint);
var a,b,i,j:longint;
begin
    i:=l; j:=r; a:=x[(l+r)div 2];
    repeat
        while x[i]>a do inc(i);
        while a>x[j] do dec(j);
        if not(i>j) then
        begin
            b:=x[i]; x[i]:=x[j]; x[j]:=b;
            b:=y[i]; y[i]:=y[j]; y[j]:=b;
            inc(i); dec(j);
        end;
    until i>j;
    if l<j then sort(l,j);
    if i<r then sort(i,r);
end;

begin
    readln(n);
    for i:=1 to n do
        readln(x[i],y[i]);
    sort(1,n); {x[i]}
    for i:=0 to n do
        for j:=0 to n+1 do
            dp[i,j]:=-1000000000;
    dp[0,1]:=0;
    for i:=1 to n do
        for j:=0 to n do
            dp[i,j]:=max(dp[i-1,j],dp[i-1,max(0,j-x[i])+1]+y[i]);   
    ans:=0;
    for i:=0 to n do
        ans:=max(ans,dp[n,i]);
    writeln(ans);
end.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值