题意:奶牛们要用K个不同类型的石头建太空电梯.每一种石头的高度为Hi,数量为Ci,且不能放在高于Ai的地方,求最高能建多高的太空电梯.
分析:多重背包,数组标记.显然将ai小的放在下面会更优.所以先排序.
code:
const maxh=41000; var cnt:array[0..maxh] of longint; h,a,c:array[0..401] of longint; f:array[0..maxh] of boolean; n,i,j,k,tmp,ans:longint; procedure swap(var a,b:longint); var tmp:longint; begin tmp:=a; a:=b; b:=tmp; end; procedure sort(l,r:longint); var i,j,mid:longint; begin i:=l; j:=r; mid:=a[(l+r)>>1]; while i<=j do begin while a[i]<mid do inc(i); while a[j]>mid do dec(j); if i<=j then begin swap(h[i],h[j]); swap(a[i],a[j]); swap(c[i],c[j]); inc(i); dec(j); end; end; if i<r then sort(i,r); if j>l then sort(l,j); end; begin readln(n); for i:=1 to n do readln(h[i],a[i],c[i]); sort(1,n); f[0]:=true; for i:=1 to n do begin fillchar(cnt,sizeof(cnt),0); for j:=0 to a[i] do if (f[j]=true)and(not f[j+h[i]])and(j+h[i]<=a[i])and(cnt[j]<c[i]) then begin f[j+h[i]]:=true; cnt[j+h[i]]:=cnt[j]+1; end; end; for i:=maxh downto 0 do if f[i] then break; writeln(i); end.