poj 1276 Cash Machine

来源:互联网 发布:期货农产品数据库建立 编辑:程序博客网 时间:2024/06/08 14:42

Description

A Bank plans toinstall a machine for cash withdrawal. The machine is able todeliver appropriate @ bills for a requested cash amount. Themachine uses exactly N distinct bill denominations, say Dk, k=1,N,and for each denomination Dk the machine has a supply of nk bills.For example,

N=3, n1=10, D1=100, n2=4, D2=50, n3=5, D3=10

means the machine has a supply of 10 bills of @100 each, 4 bills of@50 each, and 5 bills of @10 each.

Call cash the requested amount of cash the machine should deliverand write a program that computes the maximum amount of cash lessthan or equal to cash that can be effectively delivered accordingto the available bill supply of the machine.

Notes:
@ is the symbol of the currency delivered by the machine. Forinstance, @ may stand for dollar, euro, pound etc.

Input

The program input isfrom standard input. Each data set in the input stands for aparticular transaction and has the format:

cash N n1 D1 n2 D2 ... nN DN

where 0 <= cash <= 100000 is theamount of cash requested, 0 <=N <= 10is the number of bill denominations and 0 <= nk<= 1000 is the number of available bills for the Dkdenomination, 1 <= Dk <= 1000, k=1,N.White spaces can occur freely between the numbers in the input. Theinput data are correct.

Output

For each set of datathe program prints the result to the standard output on a separateline as shown in the examples below.

Sample Input

735 3  4 125  6 5  3 350633 4  500 30  6 100  1 5  0 1735 00 3  10 100  10 50  10 10

Sample Output

73563000

Hint

The first data setdesignates a transaction where the amount of cash requested is@735. The machine contains 3 bill denominations: 4 bills of @125, 6bills of @5, and 3 bills of @350. The machine can deliver the exactamount of requested cash.

In the second case the bill supply of the machine does not fit theexact amount of cash requested. The maximum cash that can bedelivered is @630. Notice that there can be several possibilitiesto combine the bills in the machine for matching the deliveredcash.

In the third case the machine is empty and no cash is delivered. Inthe fourth case the amount of cash requested is @0 and, therefore,the machine delivers no cash.

Source

Southeastern Europe 2002
 
题目大意:给出cash和n,还有n种面值的钞票,各d[i]张,求能取到不大于cash的最大价值。
 
多重背包。将每种面值的d拆成1,2,4,8,16,32,...,2^k,d[i]-2^(k+1)+1张,进行0-1背包的操作。
 
ACCODE
programpku_1276;
var f:array[0..100000] of boolean;
   d,vv,v:array[1..1000] of longint;
   n,nn,c:longint;
//============================================================================
procedure divide;
var i,j:longint;
begin
  n:=0;
  for i:=1 to nn do
  begin j:=0;
    while truedo
     if 1 shl (j+1)-1<=d[i] then
     begin
       inc(n);
       v[n]:=vv[i]*1 shl j;
       inc(j);
     end else break;
    if d[i]-1shl j+1>0 then
    begin
     inc(n);
     v[n]:=vv[i]*(d[i]-1 shl j+1);
    end;
  end;
end;
//============================================================================
procedure work;
var i,j:longint;
begin
  read(c,nn);
  for i:=1 to nn do
   read(d[i],vv[i]);
  divide;
  for i:=1 to c do f[i]:=false;
  f[0]:=true;
  for i:=1 to n do
    for j:=cdownto v[i] do
     f[j]:=f[j] or f[j-v[i]];
  for i:=c downto 0 do
    if f[i]then
    begin
     writeln(i);
     break;
    end;
end;
//============================================================================
begin
  while not(seekeof) do work;
end.
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝误点了收货怎么办 注销公司社保户怎么办 手机媒体声音小怎么办 手机媒体没有声音怎么办 vivo开不了机怎么办 vivox5开不了机怎么办 淘宝商品出现问题怎么办 淘宝企业店新店怎么办 ps中的字体模糊怎么办 电商时代我们怎么办 电商虚假宣传怎么办 有货车找不到货源怎么办 天猫订单删除怎么办 微信支付不成功怎么办 滴滴订单未支付怎么办 在天猫买了假货怎么办 13岁有痛经怎么办 新店排名被降怎么办 淘宝店铺跳失率过大怎么办 国外打印很贵怎么办 店铺访客被拒怎么办 steam锁支付后怎么办 芦荟茎太长了怎么办 网店加盟被骗怎么办 分期乐忘记账号怎么办 贴墙纸遇到插头怎么办 用了屈臣氏过敏怎么办 商品房内电箱不符合标准怎么办 淘宝上恶意退货怎么办 退货率高了怎么办 淘宝店被关了钱怎么办 买家不申请退款怎么办 被买家恶意投诉怎么办 淘宝投诉后退款怎么办 天猫投诉不成功怎么办 苹果手机打不开流量怎么办 苹果6流量打不开怎么办 苹果笔记本电脑打不开软件怎么办 苹果软件蜂窝打不开怎么办 苹果手机wifi打不开怎么办 苹果app变成韩语怎么办