Coins
Description
You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.
Input
Output
Sample Input
3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0
Sample Output
8
4
#include<stdio.h>
#include<string.h>
int a[],c[],dp[];
int max(int a,int b)
{
return a>b?a:b;
}
void CompletePack(int v,int w,int m) //完全背包
{
for(int j=v;j<=m;j++)
dp[j]=max(dp[j],dp[j-v]+w);
}
void ZeroOnePack(int v,int w,int m) //01背包
{
for(int j=m;j>=v;j--)
dp[j]=max(dp[j],dp[j-v]+w);
}
void MultiPack(int v,int w,int m,int c) //多重背包
{
if(v*c>=m) //体积乘以数量大于总体积,说明不能完全装完,相当于有无穷件,用完全背包
CompletePack(v,w,m);
else //可以装完,用01背包
{
int k=;
while(k<c) //二进制优化
{
ZeroOnePack(k*v,k*w,m);
c-=k;
k*=;
}
ZeroOnePack(c*v,c*w,m);
}
}
int main()
{
int n,i,j,m,k;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==) break;
for(i=;i<n;i++)
scanf("%d",&a[i]); //a[i]既是物体的体积,又是物体的价值
for(i=;i<n;i++)
scanf("%d",&c[i]); //c[i]是物体的数量
memset(dp,,sizeof(dp));
for(i=;i<n;i++)
{
MultiPack(a[i],a[i],m,c[i]);
}
int count=; //计数
for(i=;i<=m;i++)
if(dp[i]==i) //可以组合且不用找钱
count++;
printf("%d\n",count);
}
return ;
}
Coins的更多相关文章
- [LeetCode] Arranging Coins 排列硬币
You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...
- ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力
Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS Memory Limit:65536KB 64bit IO Fo ...
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
- csuoj 1119: Collecting Coins
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1119 1119: Collecting Coins Time Limit: 3 Sec Memo ...
- hdu 1398 Square Coins (母函数)
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)
http://poj.org/problem?id=3260 Description Farmer John has gone to town to buy some farm supplies. ...
- POJ3260The Fewest Coins[背包]
The Fewest Coins Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6299 Accepted: 1922 ...
- POJ1742 Coins[多重背包可行性]
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 34814 Accepted: 11828 Descripti ...
- LeetCode 441 Arranging Coins
Problem: You have a total of n coins that you want to form in a staircase shape, where every k-th ro ...
随机推荐
- 【Linux】lsof 命令,记一次端口占用查询
3月21日测试时,发现测试服务器启,总是报端口占用情况,察看端口占用情况 1-使用命令 netstat -tunlp |grep 端口号 差看下 这个端口被那个进程占用 我当前使用的 JBOSS 端口 ...
- BZOJ3252: 攻略
Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏. 今天他得到了一款新游戏<XX半岛>,这款游戏有n个场景 ...
- 基础DOS命令
DOS命令: 1. 切换盘符:d: 2. 查看文件目录清单: dir dir/s 查看所有目录及子目录下的所有内容 dir/p 分屏显示 dir/s/p 分屏显示所有内容 提前结束:ctr ...
- dev checkbox多选
GridControl如果要支持多选,设置Options->OptionSeletion->MultiSelet为true就ok.
- HTML 5 拖放(Drag 和drop)
浏览器支持 Internet Explorer 9.Firefox.Opera 12.Chrome 以及 Safari 5. 1.把标签 draggable 属性设置为 true. 2.向标签添加on ...
- dblink嵌套场景下 查询出现:ORACLE ORA-00600错误的解决
前段时间在做oracle查询的时候遇到了一个非常奇怪的现象,现将现象和解决过程记录下来,以备查看: 环境描述:A数据库通过dblink访问B数据库的视图,B数据库的视图的数据是通过B的dblink连接 ...
- 判断是否为IE浏览器
function isIE(){ if (!!window.ActiveXObject || "ActiveXObject" in window){ ...
- java设计模式
五种创建型模式: 1.工厂模式 普通工厂模式: 工厂类提供一个方法可以生产多种实现了某种接口的类 多方法工厂模式: 一个方法对应一个要生产的类 静态工厂模式: 静态方法来生产类 2.抽象工厂模式 工厂 ...
- 20145220&20145209&20145309信息安全系统设计基础实验报告(4)
20145220&20145209&20145309信息安全系统设计基础实验报告(4) 实验报告链接: http://www.cnblogs.com/zym0728/p/6132246 ...
- 史上最全的java随机数生成算法分享(转)
这篇文章主要介绍了史上最全的java随机数生成算法,我分享一个最全的随机数的生成算法,最代码的找回密码的随机数就是用的这个方法 String password = RandomUtil.generat ...