POJ 1742 Coins 优化后的多重背包】的更多相关文章

Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 37853   Accepted: 12849 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…
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 pri…
hdu 2844 poj 1742 Coins 题目相同,但是时限不同,原本上面的多重背包我初始化为0,f[0] = 1;用位或进行优化,f[i]=1表示可以兑成i,0表示不能. 在poj上运行时间正好为时限3000ms....太慢了,hdu直接TLE(时限1s); 之 后发现其实并不是算法的问题,而是库函数的效率没有关注到.我是使用fill()按量初始化的,但是由于memset()可能是系统底层使用了四个字节拷 贝的函数(远比循环初始化快),效率要高得多..这就是为什么一直TLE的原因,fil…
\(Coins\) \(solution:\) 这道题很短,开门见山,很明显的告诉了读者这是一道多重背包.但是这道题的数据范围很不友好,它不允许我们直接将这一题当做01背包去做.于是我们得想一想优化. $bitset $ 优化: 这个是我最先想到的,因为这道题只牵扯到了能不能买,也就是说这个背包并没有什么权值(只有"可以"和"不可以")然后就是单纯的状态转移.而这不是我们的二进制最擅长的东西吗?(我们利用某一个硬币的面额进行更新时,直接用二进制的左右移和或运算即可)…
<挑战程序设计竞赛>上DP的一道习题. 很裸的多重背包.下面对比一下方法,倍增,优化定义,单调队列. 一开始我写的倍增,把C[i]分解成小于C[i]的2^x和一个余数r. dp[i][j]的定义前i个数字能否到凑出j来,改成一位滚动数组. #include<cstdio> #include<iostream> #include<string> #include<cstring> #include<queue> #include<…
传送门 解题思路 多重背包,二进制优化.就是把每个物品拆分成一堆连续的\(2\)的幂加起来的形式,然后把最后剩下的也当成一个元素.直接类似\(0/1\)背包的跑就行了,时间复杂度\(O(nmlogc)\). 代码 #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<cstring> #include<algorithm> #inc…
http://poj.org/problem?id=1742 n个硬币,面值分别是A1...An,对应的数量分别是C1....Cn.用这些硬币组合起来能得到多少种面值不超过m的方案. 多重背包,不过这题很容易超时,用背包九讲的代码有人说行,但是我提交还是超时,后来参考别人代码加了一些优化才能过,有时间要去搞清楚多重背包的单调队列优化. #include<cstdio> #include<cstring> #include<algorithm> using namespa…
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 pri…
一.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 p…
题意 : 有 n 种面额的硬币,给出各种面额硬币的数量和和面额数,求最多能搭配出几种不超过 m 的金额? 分析 : 这题可用多重背包来解,但这里不讨论这种做法. 如果之前有接触过背包DP的可以自然想到DP数组的定义 ==> dp[i][j] 表示使用前 i 种硬币是否可以凑成面额 j . 根据这样的定义,则一开始初始化 dp[0][0] = true 最后统计 dp[n][1 ~ m] 为 true 的数量即为答案 状态转移方程为 dp[i][j] |= dp[i-1][ j - k*val[i…
题意:有n种面额的硬币.面额.个数分别为A_i.C_i,求最多能搭配出几种不超过m的金额? 思路:dp[j]就是总数为j的价值是否已经有了这种方法,如果现在没有,那么我们就一个个硬币去尝试直到有,这种价值方法有了的话,那么就是总方法数加1.多重背包可行性问题 传统多重背包三重循环会超时,因为只考虑是否可行,没有考虑剩余面额数量的因素. o(n*v)方法 #include <iostream> #include <cstdio> #include <string.h> #…
Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse 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(witho…
题目链接:POJ - 1742 题目大意 现有 n 种不同的硬币,每种的面值为 Vi ,数量为 Ni ,问使用这些硬币共能凑出 [1,m] 范围内的多少种面值. 题目分析 使用一种 O(nm) 的 DP (据说这是类多重背包?),枚举每一种硬币,对于每一种硬币 i 枚举每一个面值 j ,如果这个面值 j 使用前 i-1 种硬币已经可以凑出,就直接跳过,否则尝试加入一个硬币 i ,看是否能凑出 j .需要满足 (f[j - Vi] == true) && (UseNum[j - Vi] +…
Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10632    Accepted Submission(s): 4230 Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One…
单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn.net/flyinghearts/article/details/5898183 传送门:hdu 3401 Trade /************************************************************** Problem:hdu 3401 Trade Us…
Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3792   Accepted: 1144 Description Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motore…
Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 28448   Accepted: 9645 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…
题解 一个自然的思路是对于每一个物品做一次01背包 然后T飞了. 试着用二进制拆分,还是T了. 单调队列,对不起,懒,不想写. 我们这样想.设dp[i]代表i这个面值前几种硬币是否能凑到 然后对于每一个i,我们用used[i]代表要凑到i这个值至少要多少个当前这种硬币 然后used可以o(m)得到(当dp[i]=1时,used[i]=0,否则dp[i]=used[dp[i-a]]+1),对于一个used[i]<=c我们把dp[i]变为1. 完成了转移这样复杂度为O(n*m) #include<…
参考:http://www.hankcs.com/program/cpp/poj-1742-coins.html 题意:给你n种面值的硬币,面值为a1...an,数量分别为c1...cn,求问,在这些硬币的组合下,能够多少种面值,该面值不超过m 思路:设d[i][j]——前i种硬币,凑成总值j时,第i种硬币所剩余的个数. 默认d[i][j] = -1,代表无法凑成总值j 转移方程为,若d[i-1][j]≥0,代表前i-1种已能够凑成j,那么就不必花费第i种硬币,所以d[i][j] = c[i]…
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 c…
dp[i][j]表示前i种硬币中取总价值为j时第i种硬币最多剩下多少个,-1表示无法到达该状态. a.当dp[i-1][j]>=0时,dp[i][j]=ci; b.当j-ai>=0&&dp[i-1][j-ai]>0时,dp[i][j]=dp[i-1][j-ai]-1; c.其他,dp[i][j]=-1 Source Code Problem: User: BMan Memory: 1112K Time: 1547MS Language: G++ Result: Accep…
题意:给定n种硬币的价值和数量,问能组成1~m中多少种面值. 分析: 1.dp[j]表示当前用了前i种硬币的情况下,可以组成面值j. 2.eg: 3 10 1 3 4 2 3 1 (1)使用第1种硬币,可以组成的面值0 1 2,eg:当前cnt[2]表示组成面值2使用了两(cnt[2])个第一种硬币. (2)在使用第一种硬币基础上,使用第二种硬币,可组成0 1 2 3 6 9,eg:当前cnt[6]表示组成面值6使用了两(cnt[6])个第二种硬币,依此类推. #pragma comment(l…
id=1742" target="_blank">题目链接~~> 做题感悟:第一次做的时候用的二进制优化.可是没注意到是险过.so也没去看单调队列的解法. 解题思路: 假设你做过单调队列的题,或者看过相关的博客就好理解这题了.博客. 再加上这题体积与价值相等那么就更好做了.仅仅有 j %v[ i ] 余数同样的才干够同一时候处理(j 指的是某个体积的值),在计算某个数的时候,仅仅要计算前面的同样的余数中(在个数限制内)是否有 true(有放满的) 就能够了. 代码…
Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 32977   Accepted: 11208 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…
// v给出N种硬币和个数,问可以取到1->M中的多少个值.// 背包 完全背包 或多 重背包(二进制优化)都可以做// #include <iostream> #include <algorithm> #include <queue> #include <math.h> #include <stdio.h> #include <string.h> #include <vector> using namespace…
Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 32955   Accepted: 11199 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  HDU 2844 不能用最基础的多重背包模板:会超时的!!! 之后看了二进制优化了的多重背包. 就是把多重转变成01背包: 具体思路见:http://www.cnblogs.com/tt123/p/3280521.html #include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> using namespace std; ],a1[],a[],b[];…
Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9826    Accepted Submission(s): 3916 Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One…
(点击此处查看原题) 题意分析 给你n种不同价值的硬币,价值为val[1],val[2]...val[n],每种价值的硬币有num[1],num[2]...num[n]个,问使用这n种硬币可以凑齐[1,m]内多少价值(换句话说,就是可以恰好支付的价格有多少) 解题思路 一开始觉得这个题也不是很难,就是多重背包问题,但是用二进制优化的多重背包写法TLE后,陷入了深思... 看了数据范围,二进制优化的时间复杂度为O(∑ log(num[i]  * V),加上多组输入后....应该是没被冤枉了....…
题目代号:POJ 3260 题目链接:http://poj.org/problem?id=3260 The Fewest Coins Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6715 Accepted: 2072 Description Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he alway…