Coins
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 33269   Accepted: 11295

Description

People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.
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

The
input contains several test cases. The first line of each test case
contains two integers n(1<=n<=100),m(m<=100000).The second line
contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn
(1<=Ai<=100000,1<=Ci<=1000). The last test case is followed
by two zeros.

Output

For each test case output the answer on a single line.

Sample Input

3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0

Sample Output

8
4

题意:题目给定 n种 硬币,第i种价值为 A[i],数量为C[i] ,问这些硬币能够组成1-m中哪些硬币。 分析:这题去年在hdu 2844上面过了,利用分组背包每次将每种物品分割,分割完一次马上进行就进行 01 背包,这样处理的速度就比所有的都分割完后再进行01背包要快许多。。但是没想到poj的数据如此变态。。然后看了网上的题解,恍然大悟。。原来当A[i]*C[i]>=m时,我们就可以将第i件物品看成无穷件了。。然后只要进行完全背包就OK。 hdu 2844 AC代码:
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std; int a[],b[],value[],f[];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF&&n>)
{
int wi=;
memset(f,,sizeof(f));
for(int i=; i<n; i++)
{
scanf("%d",&a[i]);
}
for(int i=; i<n; i++)
{
scanf("%d",&b[i]);
}
for(int i=; i<n; i++)
{
int sum=;
int w=b[i],v=a[i];
while(w>)
{
if(w>=sum)
{
value[wi]=sum*v;
w=w-sum;
sum=sum*;
}
else
{
value[wi]=w*v;
w-=w;
} for(int j=m; j>=value[wi]; j--)
f[j]=max(f[j],f[j-value[wi]]+value[wi]);
wi++;
} }
int ans=;
for(int i=; i<=m; i++)
{
if(f[i]==i) ans++;
}
printf("%d\n",ans); }
return ;
}

poj 1742 AC代码:

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#define N 1005
#define M 100005
using namespace std; int A[],C[],W[N];
bool dp[M];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF,n+m)
{
memset(dp,,sizeof(dp));
for(int i=; i<=n; i++)
{
scanf("%d",&A[i]);
}
for(int i=; i<=n; i++)
{
scanf("%d",&C[i]);
}
int k = ;
dp[]=; ///初始化0元是存在的
int ans = ;
for(int i=; i<=n; i++)
{
if(C[i]*A[i]>=m) { ///如果A[i]*C[i] >= m,我们可以将其当成完全背包问题处理,因为k*A[i]不能超过
///m,超过了就可以看成"无限"了.
for(int v = A[i];v<=m;v++){
if(!dp[v]&&dp[v-A[i]]) {
dp[v]=true;
ans++;
}
}
}else{
int t = ;
while(C[i]>){
if(C[i]>=t){
W[k]=A[i]*t;
C[i]-=t;
t = t<<;
}else{W[k]=A[i]*C[i];C[i]=;}
for(int v=m;v>=W[k];v--){
if(!dp[v]&&dp[v-W[k]]) { ///当dp[v-W[k]]存在时才能够推导出dp[v]
dp[v]=true;
ans++;
}
}
k++;
}
}
}
printf("%d\n",ans); }
return ;
}
 

poj 1742(好题,楼天城男人八题,混合背包)的更多相关文章

  1. poj 1737男人八题之一 orz ltc

    这是楼教主的男人八题之一.很高兴我能做八分之一的男人了. 题目大意:求有n个顶点的连通图有多少个. 解法: 1.  用总数减去不联通的图(网上说可以,我觉得时间悬) 2.    用动态规划(数学递推) ...

  2. poj 1741 楼教主男人八题之中的一个:树分治

    http://poj.org/problem? id=1741 Description Give a tree with n vertices,each edge has a length(posit ...

  3. POJ1742 Coins(男人八题之一)

    前言 大名鼎鼎的男人八题,终于见识了... 题面 http://poj.org/problem?id=1742 分析 § 1 多重背包 这很显然是一个完全背包问题,考虑转移方程: DP[i][j]表示 ...

  4. Cogs 1714. [POJ1741][男人八题]树上的点对(点分治)

    [POJ1741][男人八题]树上的点对 ★★★ 输入文件:poj1741_tree.in 输出文件:poj1741_tree.out 简单对比 时间限制:1 s 内存限制:256 MB [题目描述] ...

  5. poj 1743 男人八题之后缀数组求最长不可重叠最长重复子串

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14874   Accepted: 5118 De ...

  6. 博弈论(男人八题):POJ 1740 A New Stone Game

    A New Stone Game Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5694   Accepted: 3119 ...

  7. nyoj137 取石子(三) 楼教主男人八题之一

    思路:一堆时,N态.两堆时,当两堆数量相同,P态,不同为N态.三堆时,先手可以变成两堆一样的,必胜N态. 此时可以总结规律:堆数为偶数可能且石子数都是两两相同的,为P态.分析四堆时,当四堆中两两数量一 ...

  8. 新男人八题---AStringGame

    终于完成进度男人1/8,为了这题学了sam= = 题意先有一个串,n个子串,两个人轮流每次在子串上加字符,要求加完后还是原串的子串,最后不能加的就是输者,求赢的人 解法:sam之后在构造的状态图上跑s ...

  9. codeforces水题100道 第十八题 Codeforces Round #289 (Div. 2, ACM ICPC Rules) A. Maximum in Table (brute force)

    题目链接:http://www.codeforces.com/problemset/problem/509/A题意:f[i][1]=f[1][i]=1,f[i][j]=f[i-1][j]+f[i][j ...

随机推荐

  1. bzoj3810: [Coci2015]Stanovi(记忆化搜索)

    实际上切出来的矩阵在原矩阵上的位置是不重要的...重要的只有矩阵的大小和上下左右是否在边界上. 于是我们可以设f[x][y][l][r][u][d]表示x*y的矩阵上下左右是不是边界的最小代价. 记忆 ...

  2. 微信小程序 事件绑定 bind和catch 区别

    转自:https://blog.csdn.net/xiaoqiang_0719/article/details/79729592 本文以冒泡事件tap(手指触摸后马上离开,也就是点击事件)为例子来区别 ...

  3. 使用Spring Animation的API创建动画

    pring Animation 是一种特殊的动画曲线,自从 iOS 7 开始被广泛应用在系统动画中. Spring Animation 是Linear(线性即匀速)动画.Ease-out(即动画减速地 ...

  4. 照片EXIF信息的读取和改写的JAVA实现

    由于项目需要对照片的EXIF信息进行处理,因此在网上搜索了一番.捣鼓出来了,写下,总结. 需要用到2个jar包,metadata-extractor-2.3.1和mediautil-1.0.这2个ja ...

  5. [cerc2012][Gym100624D]20181013

    题意:一个序列,如果存在一个连续子序列,满足该子序列中没有只存在一次的序列,则原序列为boring,否则non-boring 题解: 分治递归 对一个序列,如果找到了一个只出现一次的数位于a[x],则 ...

  6. 深入分析Spring 与 Spring MVC容器(山东数漫江湖)

    1 Spring MVC WEB配置 Spring Framework本身没有Web功能,Spring MVC使用WebApplicationContext类扩展ApplicationContext, ...

  7. DotNet 学习笔记 OWIN

    Open Web Interface for .NET (OWIN) ----------------------------------------------------------------- ...

  8. CSS浮动为什么不会遮盖同级元素

    1.问题描述 在W3CSchool学习web前端时,看完CSS定位-浮动这一节后,感觉没有什么问题.但是在CSS高级-分类这一节的中进行实践时,遇到了如下问题.测试地址:浮动的简单应用. 完整的htm ...

  9. Centos修改镜像为国内的阿里云源或者163源等国内源

    阿里安装软件镜像源 阿里云Linux安装镜像源地址:http://mirrors.aliyun.com/ 第一步:备份你的原镜像文件,以免出错后可以恢复. mv /etc/yum.repos.d/Ce ...

  10. 如何才可以干掉Cortana进程,开机不启动

    直接禁用即可WIN——设置——隐私——语音.墨迹书写和键入——停止收集有关我的信息——关闭