POJ 1742 Coins DP 01背包
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: Accepted
Source Code
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI 3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else freopen("d:\\in1.txt","r",stdin);
freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
int ch;
while((ch=getchar())!=EOF) {
if(ch!=' '&&ch!='\n')return ch;
}
return EOF;
} const int maxn=;
const int maxm=;
int c[maxn],a[maxn];
int dp[maxm]; int main()
{
//freopen("data.in","r",stdin);
int n,m;
while(cin>>n>>m)
{
if(n&&m);else break;
for(int i=;i<=n;i++)
cin>>a[i];
for(int i=;i<=n;i++)
cin>>c[i];
memset(dp,-,sizeof(dp));
dp[]=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(dp[j]>=)
dp[j]=c[i];
else if(j>=a[i]&&dp[j-a[i]]>)
dp[j]=dp[j-a[i]]-;
}
}
int cnt=;
for(int i=;i<=m;i++)
if(dp[i]>=)
cnt++;
cout<<cnt<<endl;
}
return ;
}
POJ 1742 Coins DP 01背包的更多相关文章
- HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...
- UVA 562 Dividing coins(dp + 01背包)
Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were figh ...
- Poj 1742 Coins(多重背包)
一.Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dolla ...
- POJ 1742 Coins(多重背包?)
题解 一个自然的思路是对于每一个物品做一次01背包 然后T飞了. 试着用二进制拆分,还是T了. 单调队列,对不起,懒,不想写. 我们这样想.设dp[i]代表i这个面值前几种硬币是否能凑到 然后对于每一 ...
- POJ 1742 Coins 【多重背包DP】
题意:有n种面额的硬币.面额.个数分别为A_i.C_i,求最多能搭配出几种不超过m的金额? 思路:dp[j]就是总数为j的价值是否已经有了这种方法,如果现在没有,那么我们就一个个硬币去尝试直到有,这种 ...
- POJ 1742 Coins(多重背包) DP
参考:http://www.hankcs.com/program/cpp/poj-1742-coins.html 题意:给你n种面值的硬币,面值为a1...an,数量分别为c1...cn,求问,在这些 ...
- poj 1742 Coins (多重背包)
http://poj.org/problem?id=1742 n个硬币,面值分别是A1...An,对应的数量分别是C1....Cn.用这些硬币组合起来能得到多少种面值不超过m的方案. 多重背包,不过这 ...
- POJ 1742 Coins(多重背包,优化)
<挑战程序设计竞赛>上DP的一道习题. 很裸的多重背包.下面对比一下方法,倍增,优化定义,单调队列. 一开始我写的倍增,把C[i]分解成小于C[i]的2^x和一个余数r. dp[i][j] ...
- POJ 1742 Coins 【可行性背包】【非原创】
People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony ...
随机推荐
- Visual.Studio.2013.IDE+visual.studio.15.preview5 编译器
硬盘版Visual.Studio.2013.IDE + visual.studio.15.preview5 编译器 使用前注意以下事项: 1.右键-管理员权限安装,VS15补丁.exe,补丁是VS15 ...
- java 线程的命名
//线程的命名 class xc2 extends Thread{ public void run(){ for(int i=0;i<20;i++){ //Thread.currentThrea ...
- gc之四--Minor GC、Major GC和Full GC之间的区别
针对HotSpot VM的实现,它里面的GC其实准确分类只有两大种: Partial GC:并不收集整个GC堆的模式 Young GC:只收集young gen的GC Old GC:只收集old ge ...
- LeetCode 342. Power of Four
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...
- 49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- autoit 中_GUICtrlStatusBar_SetBkColor失效的解决办法
#include <GuiConstantsEx.au3> #include <GuiStatusBar.au3> #include <WinAPI.au3> #i ...
- 解决Windows8下Cisco Systems VPN Client的Reason 442: Failed to Enable Virtual Adapter错误
Windows8下使用Cisco Systems VPN Client创建的Cisco IPSec VPN无法连接,提示Reason 442: Failed to Enable Virtual Ada ...
- 建议Javascript以后都用严格模式
建议以后都在js文件的头部加上 "use strict"; 现在主流的浏览器都支持,不支持的浏览器也会忽略掉. 可以使我们写的更规范,可控: 有些错误编译的时候就会出现,方便排错:
- Linux操作系统奥秘01-系统引导(MBR - 硬盘的0磁道)
Boot:1.系统PowerOn 2.BIOS在完成硬件初始化以及POST 3.BIOS加载MBR 4.GRUB->GRUB or kernal MBR MBR即主引导记录,是在BIOS中选择的 ...
- Mongodb数据导出工具mongoexport和导入工具mongoimport介绍
一.导出工具mongoexport Mongodb中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件.可以通过参数指定导出的数据项,也可以根据指定的条件导 ...