poj 1742(好题,楼天城男人八题,混合背包)
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 33269 | Accepted: 11295 |
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
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
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(好题,楼天城男人八题,混合背包)的更多相关文章
- poj 1737男人八题之一 orz ltc
这是楼教主的男人八题之一.很高兴我能做八分之一的男人了. 题目大意:求有n个顶点的连通图有多少个. 解法: 1. 用总数减去不联通的图(网上说可以,我觉得时间悬) 2. 用动态规划(数学递推) ...
- poj 1741 楼教主男人八题之中的一个:树分治
http://poj.org/problem? id=1741 Description Give a tree with n vertices,each edge has a length(posit ...
- POJ1742 Coins(男人八题之一)
前言 大名鼎鼎的男人八题,终于见识了... 题面 http://poj.org/problem?id=1742 分析 § 1 多重背包 这很显然是一个完全背包问题,考虑转移方程: DP[i][j]表示 ...
- Cogs 1714. [POJ1741][男人八题]树上的点对(点分治)
[POJ1741][男人八题]树上的点对 ★★★ 输入文件:poj1741_tree.in 输出文件:poj1741_tree.out 简单对比 时间限制:1 s 内存限制:256 MB [题目描述] ...
- poj 1743 男人八题之后缀数组求最长不可重叠最长重复子串
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14874 Accepted: 5118 De ...
- 博弈论(男人八题):POJ 1740 A New Stone Game
A New Stone Game Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5694 Accepted: 3119 ...
- nyoj137 取石子(三) 楼教主男人八题之一
思路:一堆时,N态.两堆时,当两堆数量相同,P态,不同为N态.三堆时,先手可以变成两堆一样的,必胜N态. 此时可以总结规律:堆数为偶数可能且石子数都是两两相同的,为P态.分析四堆时,当四堆中两两数量一 ...
- 新男人八题---AStringGame
终于完成进度男人1/8,为了这题学了sam= = 题意先有一个串,n个子串,两个人轮流每次在子串上加字符,要求加完后还是原串的子串,最后不能加的就是输者,求赢的人 解法:sam之后在构造的状态图上跑s ...
- 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 ...
随机推荐
- 【DP优化】【P1430】序列取数
传送门 Description 给定一个长为n的整数序列,由A和B轮流取数(A先取).每个人可从序列的左端或右端取若干个数(至少一个),但不能两端都取.所有数都被取走后,两人分别统计所取数的和作为各自 ...
- 【链表】【UVA11988】Broken Keyboard
传送门 明明是道黄题我竟然来写博客……我真的是什么数据结构也不会写了 Description 你在输入文章的时候,键盘上的Home键和End键出了问题,会不定时的按下.你却不知道此问题,而是专心致志地 ...
- Matrix.(POJ-2155)(树状数组)
题目是让每次对一个子矩阵进行翻转(0变1,1变0), 然后有多次询问,询问某个点是0还是1 这题可以用二维的树状数组来解决,考虑传统的树状数组是改变某个点,然后查询某一段, 而这个题是改变某一段,查询 ...
- IAR ------ 扩展关键字__weak
__weak作用:允许多个同名函数同时存在,但是最多只有一个没有__weak修饰.如果有non-weak函数(没__weak修饰),则此函数被使用,否则从__weak修饰的函数中选择其中一个. 下图来 ...
- c# 深拷贝与浅拷贝的区别分析及实例
浅拷贝(影子克隆):只复制对象的基本类型,对象类型,仍属于原来的引用. 深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对象产生的. 深拷贝是指源对象与拷贝对象互相独 ...
- Java 中 给静态方法 添加泛型 (static <T>)
今天在用到static方法的时候.想要用泛型.结果不能通过编译. 上网查了一下.其具体写法如下:
- CentOS7安装Memcached 三步曲
1.yum 安装 yum clean allyum -y updateyum -y install memcached 2.Memcached 运行 memcached -h //查看考号修改配置vi ...
- 【Atcoder】CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning
[题意]给定只含小写字母的字符串,要求分割成若干段使段内字母重组顺序后能得到回文串,求最少分割段数.n<=2*10^5 [算法]DP [题解]关键在于快速判断一个字符子串是否合法,容易发现合法仅 ...
- JS之document例题讲解1(两张表之间数据转移、日期时间选择、子菜单下拉、用div做下拉菜单、事件总结)
作业一:两个列表之间数据从一个列表移动到另一个列表 <div style="width:600px; height:500px; margin-top:20px"> & ...
- bzoj 1030 ac自动机
比较容易看出来先建立ac自动机,然后在自动机上做DP,设w[0..1][i][j]为当前不包括/包括字典中的字符串,当前在自动机中走到第i个节点,完成的文本的长度为j的方案数,那么比较容易的转移w[i ...