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 ...
随机推荐
- BZOJ4712 : 洪水
首先不难列出DP方程: $dp[x]=\min(w[x],h[x])$ $h[x]=\sum dp[son]$ 当$w[x]$增加时,显然$dp[x]$不会减少,那么我们求出$dp[x]$的增量$de ...
- Prototypes in Javascript 收集.__proto__
It’s important to understand that a function’s prototype property has nothing to do with it’s actual ...
- Shader实例:2D流光
准备: 1.一张背景图 2.一张流光图 3.一张过滤图 like this: 效果: 代码: Shader "Custom/2d_flow" { Properties { _Mai ...
- SpringMVC介绍之Validation
对于任何一个应用而言在客户端做的数据有效性验证都不是安全有效的,这时候就要求我们在开发的时候在服务端也对数据的有效性进行验证.SpringMVC自身对数据在服务端的校验有一个比较好的支持,它能将我们提 ...
- MySQL中INFORMATION_SCHEMA是什么?(2)
information_schema数据库表说明: SCHEMATA:提供了当前mysql实例中所有数据库的信息.是show databases的结果取之此表. TABLES:提供了关于数据库中的表的 ...
- *HDU1325 并查集
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- android——从零开始
一.JDK(不用安装)1.下载适合的jdk2.配置环境变量 添加一系统边=变量 JAVA_HOME=D:\Java\jdk1.8.0_91 CLASSPATH=.;%JAVA_ ...
- sweetalert api中文开发文档和手册
官网和下载地址: http://t4t5.github.io/sweetalert/ 2016年10月30日14:10:21 废话,目前php开发越来越API话,所以php方法很多都是json返回数 ...
- Onethink1.1 钩子和插件的使用!
Onethink下载请自行百度咯,安装也就几秒钟. 高手(略),只是针对和我一样需要了解的菜鸟. 主要讲一讲onethink插件的使用,因为这对我们的快速开发有帮助,所以记录一下,同时也希望能够帮助一 ...
- 张小龙在2017微信公开课PRO版讲了什么(附演讲实录和2016微信数据报告)
今天2017微信公开课PRO版在广州亚运城综合体育馆举行,这次2017微信公开课大会以“下一站”为主题,而此次的微信公开课的看点大家可能就集中在腾讯公司高级副总裁.微信之父——张小龙的演讲上了!今天中 ...