[BZOJ1531] [POI2005]Bank notes

传送门

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

题目大意

有n种面值的硬币,每个各有x[i]个,询问最少用多少枚可达到面值m

题解

多重背包,直接转化01超时,二进制优化,具体看模板里写的吧

var
 dp:array[0..20000]of longint;
 y:array[0..200,1..2]of longint;
 x:array[0..3000,1..2]of longint;
 i,j,k:longint;
 n,m,len,a:longint;
function min(a,b:longint):longint;
begin
 if a<b then exit(a) else exit(b);
end;

begin
 readln(k);
 for i:=1 to k do
  read(y[i,1]);
 for i:=1 to k do
  read(y[i,2]);
 readln(m);
 len:=0;
 for i:=1 to k do begin
  a:=1;
  while a<y[i,2] do
   begin inc(len); x[len,1]:=y[i,1]*a; x[len,2]:=a; a:=a<<1; end;
  inc(len);
  if a>y[i,2] then x[len,2]:=y[i,2]-a>>1+1 else x[len,2]:=y[i,2]-a+1;
  x[len,1]:=x[len,2]*y[i,1];
 end;
 dp[0]:=0;
 for i:=1 to m do
  dp[i]:=1000000000;
 for i:=1 to len do
  for j:=m downto x[i,1] do
   dp[j]:=min(dp[j],dp[j-x[i,1]]+x[i,2]);
 writeln(dp[m]);
 {for i:=1 to len do
  writeln(x[i,1],' ',x[i,2]);}
end.

你可能感兴趣的:([BZOJ1531] [POI2005]Bank notes)