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. 去掉vue 中的代码规范检测(Eslint验证)

    去掉vue 中的代码规范检测(Eslint验证): 1.在搭建vue脚手架时提示是否启用eslint检测的. Use ESLint to lint your code? 写 no; 2.如果项目已经生 ...

  2. 51nod 1296 有限制的排列(DP)

    对于一个i,如果要比邻居大,那么i比i-1大,i+1比i小,比邻居小同理.设v[i]=0表示i与i-1的关系无限制,v[i]=1表示a[i-1]>a[i],v[i]=2表示a[i-1]<a ...

  3. pg_basebackup: invalid tar block header size

    问题: 在使用pg_basebackup搭建备节点时,由于pg_basebackup本身使用的是int整型来保存传输的数据大小,当传输的数据大于4G的话,整数就会溢出,进而报出:pg_baseback ...

  4. Qt ------ 初始化构造函数参数,parent

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setup ...

  5. js获取当前页面的参数,带完善~~~

    let url = window.location.href; let id = url.slice(url.indexOf('?') + 4);

  6. redis初试Not all 16384 slots are covered by nodes

    按照这里的步骤玩redis集群,http://www.redis.cn/topics/cluster-tutorial.html ./src/redis-trib.rb create --replic ...

  7. PHP扩展--Oracle客户端(oci8)安装

    下载Oracle客户端 官方下载地址: Linux X86-64 同意协议,下载以下文件: oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm ...

  8. Sass 基本函数

    Sass 中的常用函数 一.字符串函数 1. unquote($string): 删除字符串前后的引号,删除一对引号,如果这个字符串没有带有引号,将返回原始的字符串. 示例: .text1 { con ...

  9. 【hdu1255】线段树求矩形面积交

    题意大概就是上图这个样子.<=100组测试数据,每组<=1000个矩形. 题解: 这个问题怎么解决..做了上一题矩形面积并应该就会了.. 对于每个节点维护3个值: cnt:该节点所代表的这 ...

  10. 【bzoj1593-预定旅馆】线段树维护连续区间

    题解: 这题非常经典啊似乎..经典模型要记住啊.. 对于每个节点维护该区间里的最大的连续区间,然后我们就可以logn递归找最前面的一段. 那就维护mx(无限制),lmx(必须从左边开始),rmx(必须 ...