poj1742 Coins【多重背包】【贪心】
| Time Limit: 3000MS | Memory Limit: 30000K | |
| Total Submissions:43969 | Accepted: 14873 |
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
Source
题意:有n种硬币,每一枚有一个价值和个数。现在取出一些硬币,面值相加得到结果S。问1~m之间可以得到多少种结果S
思路:硬币为物品,面值为体积,m为背包总容积。一次考虑每种硬币是否被用于拼成最终的面值,以“已经考虑过的物品种数”i作为DP的阶段。阶段i时,dp[j]表示前i种硬币能否拼成面值j。
但是这道题只关注“可行性”而不是“最优性”,可以发现前i种硬币能够拼成面值j只有两种可能。1、前i-1种就可以拼成面值j 2、使用了第i种硬币,发现dp[j-ai]为true,从而dp[j]变为true
于是就有一种贪心策略:设used[j]表示dp[j]在阶段i时为true至少要用到多少枚第i种硬币,并尽量选择第一种情况。在dp[j-ai]为true时,如果dp[j]已经为true,则不执行dp转移,并令used[j]=0。否则执行dp[j] = dp[j] or dp[j - ai]的转移,并令used[j] = used[j - ai] + 1
多重背包问题可以将物品拆分变成01背包问题。拆分方法有直接拆分法,二进制拆分法和单调队列。
二进制拆分法是把数量为Ci的第i种物品拆分成p+2个物品,p是满足2^0 + 2^1 + 2^2 + ... + 2^p <= Ci的最大的整数。
他们的体积分别为2^0*Vi, 2^1*Vi, ..., 2^p*Vi, Ri * Vi, 其中Ri= Ci - 2^0 - 2^1 - 2^2 - ... - 2^p
//#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<map> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL; int n, m;
const int maxn = ;
const int maxm = 1e5 + ;
int a[maxn], c[maxn];
int used[maxm];
bool dp[maxm]; int main()
{
while(scanf("%d%d", &n, &m) != EOF && (n || m)){
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[] = true;
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]){
dp[j] = true;
used[j] = used[j - a[i]] + ;
}
}
} int ans = ;
for(int i = ; i <= m; i++){
if(dp[i])ans++;
}
printf("%d\n", ans);
}
return ;
}
poj1742 Coins【多重背包】【贪心】的更多相关文章
- $POJ1742\ Coins$ 多重背包+贪心
Vjudge传送门 $Sol$ 首先发现这是一个多重背包,所以可以用多重背包的一般解法(直接拆分法,二进制拆分法...) 但事实是会TLE,只能另寻出路 本题仅关注“可行性”(面值能否拼成)而不是“最 ...
- POJ1742 Coins[多重背包可行性]
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 34814 Accepted: 11828 Descripti ...
- POJ1742:Coins(多重背包)
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...
- POJ 3260 The Fewest Coins(多重背包+全然背包)
POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...
- HDU-2844 Coins(多重背包)
Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. On ...
- POJ3260——The Fewest Coins(多重背包+完全背包)
The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very eff ...
- POJ 1742 Coins(多重背包, 单调队列)
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...
- HDU2844 Coins 多重背包
Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- Codeforces 755 F. PolandBall and Gifts 多重背包+贪心
F. PolandBall and Gifts It's Christmas time! PolandBall and his friends will be giving themselves ...
- HDu -2844 Coins多重背包
这道题是典型的多重背包的题目,也是最基础的多重背包的题目 题目大意:给定n和m, 其中n为有多少中钱币, m为背包的容量,让你求出在1 - m 之间有多少种价钱的组合,由于这道题价值和重量相等,所以就 ...
随机推荐
- (原创)android6.0系统 PowerManager深入分析(很具体)
概述 一直以来,电源管理是电子产品设计中很重要的环节.也是不论什么电子设备中最为重要的系统模块之中的一个,优秀的电源管理方案.可以提供持久的续航能力,良好的用户体验.更能提升电子产品的竞争力. 移动设 ...
- atitit。mssql sql server 转换mysql 及 分页sql ast的搭建
atitit.mssql sql server 转换mysql 及 分页sql ast的搭建 1. 主要的的转换::函数的转换,分页的转换 1 2. 思路::mssql sql >>as ...
- 以byte方式讀取SPD的數據(SMBus Controller在PCH中)
Reading A Byte of SMBus EEPROM DataThe following steps have to be followed for the System BIOS to re ...
- [svc]linux buffer和cache的区别
通俗理解buffer,cache Cache:缓存区,是高速缓存,是位于CPU和主内存之间的容量较小但速度很快的存储器,因为CPU的速度远远高于主内存的速度,CPU从内存中读取数据需等待很长的时间,而 ...
- ZOJ 2610 Puzzle 模拟
大模拟:枚举6个方向.检查每一个0是否能移动 Puzzle Time Limit: 2 Seconds Memory Limit: 65536 KB Little Georgie likes ...
- Linux Ubuntu 打开.exe文件
这两天在编译Android源码,进行到要在Linux里安装烧录软件那一步,要先装驱动,故了解了如何在linux下打开.exe文件. .exe 文件在linux下不能直接打开,可有两种方式打开:. 1. ...
- [Linux]read/write和fread/fwrite有什么区别
转自:http://blog.csdn.net/xiaofei0859/article/details/51145051 二者都是对文件进行操作,那么二者有什么区别,用的时候该如何选择呢? 1. 区别 ...
- Trie树 + DFS - CSU 1457 Boggle
Boggle Problem's Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1457 Mean: 给定n个串,有m个询问. 每个询问 ...
- C++ 指针引用
//指针引用 #include<iostream> using namespace std; struct Teacher{ ]; int age; }; int InitA(Teache ...
- php -- strpos,stripos,strrpos,strripos,strstr,strchr,stristr,strrchr
strpos() 函数 语法: mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) 查找 needle 在 h ...