【转载】poj 1276 Cash Machine 【凑钱数的问题】【枚举思路 或者 多重背包解决】
转载地址:http://m.blog.csdn.net/blog/u010489766/9229011
题目链接:http://poj.org/problem?id=1276
//算法就是枚举,看0~cash的钱是不是都能到,到了是1,否则为0
#include<stdio.h>
#include<string.h> int cash,N,n[20],d[20],dp[100100];
int main()
{
while(scanf("%d",&cash) != EOF)
{
memset(n,0,sizeof(n));
memset(d,0,sizeof(d));
memset(dp,0,sizeof(dp));
int count = 0;
dp[0] = 1;
scanf("%d",&N);
for(int i = 1; i <= N ; i++)
scanf("%d%d",&n[i],&d[i]);
for(int i = 1; i <= N ; i++)
{
for(int k = cash; k >= 0 ; k--)//N的范围较小,所以直接枚举
{
if(dp[k] == 1)
{
for(int j = 1; j <= n[i] ; j ++)
{
if(k+j*d[i] <= cash)//不加的话可能会超出数组的范围
dp[k+j*d[i]] = 1;//此处可以直接等于1,因为更新k以上的,k以下的还是上一次的1
}
}
}
} for(int i = cash; i >= 0 ; i --)
if(dp[i] == 1)
{
printf("%d\n",i);
break;
}
}
return 0;
}
解析2:多重背包,把每种钱的张数按照二进制分开,例如13分为1,2,4,6,(之所以按照二进制是因为这么分的话,小于13任何数都可由1,2,4,6组合而成),然后按照01背包搞定。
#include<stdio.h>
#include<string.h> int cash,n,v[10100],dp[101000];//数组要开的足够大
int main()
{
while(scanf("%d",&cash) != EOF)
{
memset(dp,0,sizeof(dp));
memset(v,0,sizeof(v));
scanf("%d",&n);
int cnt = 0;
for(int i = 1; i <= n ; i ++)
{
int a,b,t = 1;
scanf("%d%d",&b,&a);
if(b != 0)
{
while(t < b)//此处是把b按照二进制分开
{
b = b - t;
v[cnt++] = a * t;
t *= 2;
}
v[cnt++] = b*a;
}
}
if(n == 0 || cash == 0)
{
printf("0\n");
continue;
} dp[0] = 1;
for(int i = 0 ; i < cnt ; i ++)
for(int j = cash ; j >= v[i] ; j --)
dp[j-v[i]] == 1?dp[j] = 1:0; for(int i = cash; i >= 0;i--)
{
if(dp[i] == 1)
{
printf("%d\n",i);
break;
}
}
}
return 0;
}
【转载】poj 1276 Cash Machine 【凑钱数的问题】【枚举思路 或者 多重背包解决】的更多相关文章
- POJ 1276 Cash Machine(多重背包的二进制优化)
题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...
- Poj 1276 Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26172 Accepted: 9238 Des ...
- poj 1276 Cash Machine(多重背包)
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33444 Accepted: 12106 De ...
- POJ 1276 Cash Machine
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24213 Accepted: 8476 Descrip ...
- POJ 1276 Cash Machine(单调队列优化多重背包)
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38986 Accepted: 14186 De ...
- POJ 1276 Cash Machine 【DP】
多重背包的模型,但一开始直接将N个物品一个一个拆,拆成01背包竟然T了!!好吧OI过后多久没看过背包问题了,翻出背包九讲看下才发现还有二进制优化一说........就是将n个物品拆成系数:1,2,4, ...
- POJ 1276 Cash Machine -- 动态规划(背包问题)
题目地址:http://poj.org/problem?id=1276 Description A Bank plans to install a machine for cash withdrawa ...
- [poj 1276] Cash Machine 多重背包及优化
Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...
- POJ 1276 Cash Machine(完全背包模板题)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44409 Accepted: 16184 Description A B ...
随机推荐
- 继续聊WPF——动态数据模板
我为啥称之为“动态数据模板”?先看看下面的截图,今天,我们就是要实现这种功能. 大概是这样的,我们定义的DataTemplate是通过触发器动态应用到 ComboBoxItem 上. 这个下拉列表控件 ...
- NewtonSoft.Json NULL转空字符串
from:http://www.cnblogs.com/hetuan/articles/4565702.html NewtonSoft.Json对需要转为JSON字符串的对象的NULL值以及DBNul ...
- 如何在xampp中运行php文件 (WINDOWS)
from:http://blog.sina.com.cn/s/blog_5e87822501011os2.html 入门: 如何在xampp中运行php文件 (WINDOWS) 安装XAMPP启动ap ...
- 41、Android中当数据库需要更新时我们该怎么办?
转载 http://blog.csdn.net/jiangwei0910410003/article/details/39670813
- Digit Division
Digit Division Time limit: 1 s Memory limit: 512 MiB We are given a sequence of n decimal digits. Th ...
- ASP.NET通过代码给TextBox添加事件(点击显示日历)
private void BindDate() { tbApplyStartDate.Attributes.Add("onclick", "new Calendar(). ...
- crontab 问题分析 - CSDN博客 https://blog.csdn.net/tengdazhang770960436/article/details/50997297
cd /mnt/tools/trunk/plugins/personas; python update_keywords.py crontab 问题分析 crontab 问题分析 - CSDN博客 ...
- AngularCli项目中添加字体图标(Font)详解
本文主要讲如何在AngularCli生成的项目中使用字体图标. 一 SVG图标准备 将需要转换为字体图标的图片转换为SVG格式. 这个让项目视觉设计师搞定即可. 二 SVG图标转Font 可以通过Ic ...
- win10笔记本触摸板手势大全
- asp.net 利用Response.Filter 获取输出内容, 变更输出内容
重写 Response.Filter 就可以获取或更新输出到浏览器的内容 资料: https://weblog.west-wind.com/posts/2009/Nov/13/Captur ...