POJ1276 Cash Machine 【解法二】

来源:互联网 发布:简单冒泡排序算法 编辑:程序博客网 时间:2024/06/05 18:16

Description A Bank plans to install a machine for cash withdrawal. The
machine is able to deliver appropriate @ bills for a requested cash
amount. The machine 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 deliver and
write a program that computes the maximum amount of cash less than or
equal to cash that can be effectively delivered according to the
available bill supply of the machine.

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

Input The program input is from standard input. Each data set in the
input stands for a particular transaction and has the format:

cash N n1 D1 n2 D2 … nN DN

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

Output For each set of data the program prints the result to the
standard output on a separate line as shown in the examples below.

解法一见【这里】
单调队列优化多重背包。
但是由于不是求最优解,只是判断可行性,所以实际上不需要建队列,只保存最近的可行解即可。

#include<cstdio>#include<cstring>const int oo=0x3f3f3f3f;bool dp[15][100010];int main(){    int i,j,k,m,n,p,q,x,y,z,v,num,hd,tl;    while (scanf("%d%d",&m,&n)==2)    {        memset(dp,0,sizeof(dp));        dp[0][0]=1;        for (i=1;i<=n;i++)        {            scanf("%d%d",&num,&v);            for (j=0;j<v;j++)            {                p=-oo;                for (k=0;k*v+j<=m;k++)                {                    if (k-p<=num) dp[i][k*v+j]=dp[i-1][p*v+j];                    dp[i][k*v+j]|=dp[i-1][k*v+j];                    if (dp[i-1][k*v+j]) p=k;                }            }        }        for (i=m;;i--)          if (dp[n][i])          {            printf("%d\n",i);            break;          }    }}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 网页游戏占cpu高怎么办 微信有余额绑定其他名下怎么办 银行卡绑定支付余额不对怎么办 可乐机不制冷了怎么办 被淘宝卖家威胁怎么办 征信不好想贷款怎么办 急用钱征信不好怎么办 急用钱逾期不还怎么办 急用钱借款不还怎么办 做b超按压疼痛怎么办 做b超没有尿怎么办 b超憋不到尿怎么办 做b超前没有尿意怎么办 肝胆b超前喝水了怎么办 肝胆彩超喝水了怎么办 胆囊b超喝了水怎么办 系统b超照不到怎么办 思维彩超宝宝不动怎么办 怀孕七个月胎儿缺氧怎么办 怀孕29周小孩偏小怎么办 孕29周胎儿臀位怎么办 刚怀孕有囊肿该怎么办 食杏中毒怎么办吃什么 猫吃了扁桃仁怎么办 夏天来了??点狐臭怎么办? 我有一小点狐臭怎么办 淘客网站被微信屏蔽怎么办 微信老是屏蔽网站怎么办 华帝售后没人理怎么办 新开的网店没生意怎么办 淘小二介入买家举证不全怎么办 新股申购中签后钱不够怎么办 买的东西收不到怎么办? 拼多多按错收货怎么办 团购招生做到一半不如意怎么办 网购付款后卖家没有货怎么办 淘宝退货商家不同意退款怎么办 淘宝确认收货后卖家拒绝退款怎么办 淘宝卖家不同意退款怎么办 淘宝仅退款卖家不处理怎么办 淘宝申请退款卖家不处理怎么办