(男人八题)多重背包 POJ 1743 Coins
来源:互联网 发布:手机扒谱软件 编辑:程序博客网 时间:2024/06/06 15:53
Coins
Time Limit: 3000MSMemory Limit: 30000KTotal Submissions: 25919Accepted: 8768
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.
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 101 2 4 2 1 12 51 4 2 10 0
Sample Output
84
Source
LouTiancheng@POJ
题意:给一系列物品,每种物品价值为A,数量为C ,给定一个m , 问用这些物品组合得出的价值在m内的有多少种
思路:
我用的是二进制多重背包,一开始果断超时...... 然后我只能想到各种常数优化......一开始的时候价值为0,然后开始扩展可能的价值,这里有点像bfs吧....然后搜出m/2种可能的组合之后,我们就不用这种方法搜了,我们开始反着来看,我们考虑还没组合出的价值,把剩下没组合过的容量,分出来再一个个考虑,最理想的当然是确认一个容量是可能的之后,就直接把他删掉,一直操作到物品用完了,然后剩下还没能组合出来的,就是不能组合出的容量,由于我不知道有什么办法能常数时间内删掉一个元素,(我想用链表的,但是好像更慢),于是这里我就用了lazy的思想,当标记的组合达到剩下的没能组合出的容量个数的一半时,才开始把还没组合出的容量再一次分出来,继续以上操作。
反正想法略搓,将就下吧。。。
代码:
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<string>
#include<deque>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<stack>
#include<set>
#include<list>
using namespace std;
const int MAX = 100000+10;
#define MOD 99997
const int inf = 0xffffff;
bool dp[MAX];
int cur[MAX];
int rest[MAX];
int sz;
int num;
int A[MAX];
int w[MAX];
int n,m;
void MulPack(int amount,int C)
{
int k = 1;
while (k<amount)
{
w[num++] = k*C;
amount -= k;
k *= 2;
}
w[num++] = amount*C;
}
int main()
{
while (scanf("%d%d",&n,&m) , n || m)
{
for (int i = 0 ; i < n ; ++i)
scanf("%d",A+i);
memset(dp,0,sizeof(dp));
sz = num = 0;
cur[sz++] = 0;
dp[0] = true;
for (int i = 0 ; i < n ; ++i)
{
int k;
scanf("%d",&k);
MulPack(k,A[i]);
}
int j = 0;
while ( j < num)
{
int tem_sz = sz;
for (int i = 0 ; i < tem_sz ; ++i) if (cur[i]+w[j]<=m && !dp[cur[i]+w[j]])
{
cur[sz++] = cur[i]+w[j];
dp[cur[i]+w[j]] = true;
}
++j;
if (sz>=m/2) break;
}
int rest = m-sz;
sz = 0;
for (int i = 1 ; i <= m ; ++i) if (!dp[i])
cur[sz++] = i;
int sign = 0;
while ( j < num)
{
if (sign>=rest/2)
{
int tem_sz = sz;
sz = 0;
for (int i = 0 ; i < tem_sz ; ++i) if (!dp[cur[i]])
cur[sz++] = cur[i];
rest -= sign;
sign = 0;
}
for (int i = sz-1 ; i >= 0 ; --i) if (!dp[cur[i]])
{
if (cur[i]-w[j]>=0 && dp[cur[i]-w[j]])
{
dp[cur[i]] = true;
++sign;
}
}
++j;
}
int cnt = 0;
for (int i = 1 ; i <= m ; ++i) if (dp[i])
++cnt;
printf("%d\n",cnt);
}
}
0 0
- (男人八题)多重背包 POJ 1743 Coins
- [POJ 1742] Coins 男人八题(多重背包)
- POJ 1742 Coins(多重背包变型,楼教主男人八题)
- POJ1742.Coins(背包DP && 男人八题)
- POJ-1742-Coins-DP-男人八题
- POJ 1742 Coins 多重背包入门题
- Poj 1742 Coins(多重背包)
- poj 1742 Coins (多重背包)
- POJ 1742 - Coins (dp 多重背包)
- POJ 1742 Coins(多重背包)
- poj 1742 Coins(多重背包)
- POJ 1742 Coins (多重背包)
- poj-1742 Coins(多重背包优化)
- POJ - 1742 Coins(多重背包)@
- hdu 2844 Coins 多重背包模板题 ,二进制优化。据说是楼教主的男人八题之一
- 男人八题coins
- poj 1742 Coins(多重背包)
- POJ-3260 Coins 多重背包
- (中等) 线段树 HDU 3308 LCIS
- (简单) 搜索 HOJ 1105 Egyptian Multiplication
- (简单) 搜索 最短路 HOJ 1121 Erdos Numbers
- (基础) 树形dp HDU 1520 Anniversary party
- (男人八题之一)树形dp poj 1741 Tree
- (男人八题)多重背包 POJ 1743 Coins
- ACM牛人给的新手建议
- (中等) 树形dp POJ 1155 TELE
- (简单) 树形背包 HDU 1561 The more, The Better
- *(中等) 树形dp POJ 2486 Apple Tree
- (中等) 树形分组背包 HDU 4003 Find Metal Mineral
- (中等) 树形dp(分组背包) POJ 3345 Bribing FIPA
- C++创建对象的两种方法
- UBUNTU linux 批量删除文件
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
泸州医学院附属医院
泸州老窖特曲价格
泸州老窖股票股吧
泸州老窖特曲52度价格
泸州老窖精品头曲
泸州医学院录取分数线
泸州龙马潭区
泸州邮政编码
泸州王氏集团女儿王燕
泸州人事考试信息网
泸州问政平台
泸州连锁项目排行榜
泸州老窖特曲52价格
泸州人事考试网首页
成都到泸州高铁
泸州一日游景点推荐
百年泸州老窖
泸州有什么好玩的
泸州旅游景点一日游
泸州房产信息网
泸州客运中心
泸州好玩的地方
泸州酒价格表和图片
泸州在线招聘
泸州小蚂蚁招聘信息网
泸州人才网招聘信息
泸州天天招聘找工作
泸州市小蚂蚁招聘人才市场
泸州天天招聘
小蚂蚁招聘泸州
泸州招聘信息网
泸州小蚂蚁人才招聘网
泸州周边度假村
泸州公务员考试培训
泸州职业学校
泸州网站建设
泸州医院有哪些
泸州事业单位考试
泸州心理咨询
泸州培训学校
泸州装饰公司哪家好