POJ1742Coins(多重背包)
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 32309 | Accepted: 10986 |
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 题意:给出3种硬币的面额和数量,能拼成不大于m的多少种;
多重背包可解,因为只要求行或不行就可以了,所以就两种状态在01和完全背包的时候没必要求可行解,只要确定行或不行就ok了,所以直接与dp[j - a[i]] 或运算,
注意的是,位运算真的好快,把dp设成int,用关系运算||,是超时的,改成位运算的|直接3000ms卡过;
改成bool型直接2204ms;
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAX = + ;
bool dp[MAX];
int a[ + ],c[ + ];
int n,m;
void ZeroOnePage(int cost)
{
for(int i = m; i >= cost; i--)
{
dp[i] |= dp[i - cost];
}
}
void CompletePage(int cost, int mount)
{
for(int i = cost; i <= m; i++)
dp[i] |= dp[i - cost];
}
void MultiplePage(int cost, int mount)
{
if(cost * mount >= m)
{
CompletePage(cost, mount);
return ;
}
int k = ;
while(k < mount)
{
ZeroOnePage(k * cost);
mount -= k;
k <<= ;
}
if(mount > )
ZeroOnePage(mount * cost);
return ;
}
int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
if(n == && m == )
break;
for(int i = ; i <= n; i++)
scanf("%d", &a[i]);
for(int i = ; i <= n; i++)
scanf("%d", &c[i]);
memset(dp, , sizeof(dp));
dp[] = ;
for(int i = ; i <= n; i++)
if(c[i])
MultiplePage(a[i], c[i]);
int sum = ;
for(int i = ; i <= m; i++)
if(dp[i])
sum++;
printf("%d\n",sum);
} return ;
}
多重背包好理解
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAX = + ;
bool dp[MAX];
int a[ + ],c[ + ];
int n,m;
void ZeroOnePage(int cost)
{
for(int i = m; i >= cost; i--)
{
dp[i] |= dp[i - cost];
}
}
void CompletePage(int cost, int mount)
{
for(int i = cost; i <= m; i++)
dp[i] |= dp[i - cost];
}
void MultiplePage(int cost, int mount)
{
if(cost * mount >= m)
{
CompletePage(cost, mount);
return ;
}
int k = ;
while(k < mount)
{
ZeroOnePage(k * cost);
mount -= k;
k <<= ;
}
//这里是还剩下的mount
if(mount > )
ZeroOnePage(mount * cost);
return ;
}
int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
if(n == && m == )
break;
for(int i = ; i <= n; i++)
scanf("%d", &a[i]);
for(int i = ; i <= n; i++)
scanf("%d", &c[i]);
memset(dp, , sizeof(dp));
dp[] = ;
for(int i = ; i <= n; i++)
if(c[i])
MultiplePage(a[i], c[i]);
int sum = ;
for(int i = ; i <= m; i++)
if(dp[i])
sum++;
printf("%d\n",sum);
} return ;
} 多重背包好理解
这种解法看不懂
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAX = + ;
int dp[MAX],used[MAX],a[ + ],c[ + ];
int n,m;
int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
if(n == && m == )
break;
for(int i = ; i <= n; i++)
scanf("%d", &a[i]);
for(int i = ; i <= n; i++)
scanf("%d", &c[i]);
memset(dp, , sizeof(dp));
dp[] = ;
int sum = ;
for(int i = ; i <= n; i++)
{
memset(used, , sizeof(used));
for(int j = a[i]; j <= m; j++)
{
if(dp[j] == && dp[j - a[i]] && used[j - a[i]] < c[i])
{
sum++;
dp[j] = ;
used[j] = used[j - a[i]] + ;
}
}
}
printf("%d\n",sum);
} return ;
}
POJ1742Coins(多重背包)的更多相关文章
- 洛谷P1782 旅行商的背包[多重背包]
题目描述 小S坚信任何问题都可以在多项式时间内解决,于是他准备亲自去当一回旅行商.在出发之前,他购进了一些物品.这些物品共有n种,第i种体积为Vi,价值为Wi,共有Di件.他的背包体积是C.怎样装才能 ...
- HDU 2082 找单词 (多重背包)
题意:假设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26.那么,对于给定的字母,可以找到多少价值<=50的 ...
- Poj 1276 Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26172 Accepted: 9238 Des ...
- poj 1276 Cash Machine(多重背包)
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33444 Accepted: 12106 De ...
- (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)
http://poj.org/problem?id=3260 Description Farmer John has gone to town to buy some farm supplies. ...
- (多重背包+记录路径)Charlie's Change (poj 1787)
http://poj.org/problem?id=1787 描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie dri ...
- 单调队列优化DP,多重背包
单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...
- POJ1742 Coins[多重背包可行性]
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 34814 Accepted: 11828 Descripti ...
- POJ1276Cash Machine[多重背包可行性]
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32971 Accepted: 11950 De ...
随机推荐
- FZU软工实践–团队成员交换交接情况
0.前言 本次交接主要是为了之后Beta冲刺工作可以更加顺利而进行的.因为我跟其他队友交互不够方便,而且我们项目当前比较缺做前端的人员,后端功能已基本实现.所以我换出来了.希望大家可以顺顺利利做完一步 ...
- PDF文档打印太慢怎么办
如下图,用Adobe Acrobat打开PDF文件,然后[高级]-打勾[作为图像打印]即可
- git忽略本地文件
一.忽略文件的常用方法 1.git通常在.gitignore文件进行配置,来忽略本地文件.但是这仅对于重来没有提交过的文件有效. 2.使用git update-index --skip-worktre ...
- python拉格朗日插值
#拉格朗日插值代码 import pandas as pd #导入数据分析库Pandas from scipy.interpolate import lagrange #导入拉格朗日插值函数 inpu ...
- PostgreSQL之Sequence序列(转)
本文转载自:https://blog.csdn.net/omelon1/article/details/78798961 Sequence序列 Sequence是一种自动增加的数字序列,一般作为行或者 ...
- Delphi 2010 3513正式版破解
Delphi 2010 3513正式 一.下载ISO文件右键迅雷下载 二.安装完成后,先运行一下程序,程序会弹出叫你注册对话框,逐步点击Cancel---->OK----->No----& ...
- 英国电信反悔华为是唯一真正的5G供应商
导读 英国电信反悔华为是唯一真正的5G供应商 英国电信的一位发言人表示,该公司目前正「从我们自 2006 年以来实施的网络架构原则中,从我们的 3G 和 4G 网络核心提取华为设备」. 英国电信已经不 ...
- fio 磁盘性能
/fio -ioengine=libaio -bs=32k -direct=1 -thread -rw=randrw -percentage_random=100,0 -size=10G -filen ...
- BZOJ4864[BeiJing 2017 Wc]神秘物质——非旋转treap
题目描述 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子.这 一天, 小诚刚从研究所得到了一块奇异的陨石样本, 便迫不及待地开始 ...
- 洛谷P3959 宝藏(NOIP2017)(状压DP,子集DP)
洛谷题目传送门 Dalao的题解多数是什么模拟退火.DFS剪枝.\(O(3^nn^2)\)的状压DP之类.蒟蒻尝试着把状压改进了一下使复杂度降到\(O(3^nn)\). 考虑到每条边的贡献跟它所在的层 ...