USACO Money Systems Dp 01背包
一道经典的Dp..01背包
定义dp[i] 为需要构造的数字为i 的所有方法数
一开始的时候是这么想的
for(i = 1; i <= N; ++i){
for(j = 1; j <= V; ++j){
if(i - a[j] > 0){
dp[i] += dp[i - a[j]];
}
}
}
状态存在冗余, 输出的时候答案肯定不对
但只需要改一下两个for循环的顺序即可。
Source Code:
/*
ID: wushuai2
PROG: money
LANG: C++
*/
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int M = ;
const ll P = 10000000097ll ;
const int INF = 0x3f3f3f3f ;
const int MAX_N = ;
const int MAXSIZE = ; ofstream fout ("money.out");
ifstream fin ("money.in"); int V, N, a[];
ll dp[]; int main(){
int i, j, k, l, m, n, t, s, c, w, q, num;
fin >> V >> N;
for(i = ; i <= V; ++i){
fin >> a[i];
}
dp[] = ;
for(i = ; i <= V; ++i){
for(j = a[i]; j <= N; ++j){
if(j - a[i] >= ){
dp[j] += dp[j - a[i]];
}
}
}
/*
for(i = 1; i <= N; ++i){
for(j = 1; j <= V; ++j){
if(i - a[j] > 0){
dp[i] += dp[i - a[j]];
}
}
}
*/
fout << dp[N] << endl; fin.close();
fout.close();
return ;
}
USACO Money Systems Dp 01背包的更多相关文章
- HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...
- POJ.3624 Charm Bracelet(DP 01背包)
POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...
- HDOJ(HDU).2602 Bone Collector (DP 01背包)
HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...
- UVA.10130 SuperSale (DP 01背包)
UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...
- poj 2923 状压dp+01背包
好牛b的思路 题意:一系列物品,用二辆车运送,求运送完所需的最小次数,两辆车必须一起走 解法为状态压缩DP+背包,本题的解题思路是先枚举选择若干个时的状态,总状态量为1<<n,判断这些状态 ...
- DP(01背包) UESTC 1218 Pick The Sticks (15CCPC C)
题目传送门 题意:长度为L的金条,将n根金棍尽可能放上去,要求重心在L上,使得价值最大,最多有两条可以长度折半的放上去. 分析:首先长度可能为奇数,先*2.然后除了两条特殊的金棍就是01背包,所以dp ...
- hihoCoder#1055 : 刷油漆 (树形DP+01背包)
题目大意:给一棵带点权的树,现在要从根节点开始选出m个连通的节点,使总权值最大. 题目分析:定义状态dp(u,m)表示在以u为根的子树从根节点开始选出m个点连通的最大总权值,则dp(u,m)=max( ...
- UVA 562 Dividing coins(dp + 01背包)
Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were figh ...
随机推荐
- 使得fiddler来抓包查看微信浏览器的网页源码
需要工具:http://www.telerik.com/fiddler 下载安装后 第二步: 打开这个选项: 设置代理:allow remote computer to connect 端口为888 ...
- poj 2074 Line of Sight 计算几何
/** 大意:给定一个建筑--水平放置,给定n个障碍物, 给定一条街道,从街道上能看到整个建筑的最长的连续的区域 思路: 分别确定每一个障碍物所确立的盲区,即----建筑物的终点与障碍物的起点的连线, ...
- java.lang.VerifyError: Inconsistent stackmap frames at branch target
-XX:-UseSplitVerifier解决. 附带网址:http://stackoverflow.com/questions/12774672/java-7-inconsistent-stackm ...
- Dropping tests(01分数规划)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8176 Accepted: 2862 De ...
- ORACLE 使用RMAN管理归档日志 archived log
oracle 归档日志通常使用rman进行管理,作为备份集的一部分 正常情况下,可以根据方法删除 1 过期策略 crosscheck archivelog all; delete expired ar ...
- sign a third-party dll which don't have a strong name
Problem: Assembly generation failed -- Referenced assembly '' does not have a strong name Cause: thi ...
- 碰撞缓冲效果的导航条 js
- BZOJ 1059: [ZJOI2007]矩阵游戏( 匈牙利 )
只要存在N个x, y坐标均不相同的黑格, 那么就一定有解. 二分图匹配, 假如最大匹配=N就是有解的, 否则无解 ------------------------------------------- ...
- 移除Strorefront站点footer上的Storefront Design By WooThemes字样
进入wp-content\themes\storefront\inc\structure\footer.php, 注释掉代码: if ( ! function_exists( 'storefront_ ...
- PHP 8大安全函数
1. mysql_real_escape_string() 这个函数对于在PHP中防止SQL注入攻击很有帮助,它对特殊的字符,像单引号和双引号,加上了“反斜杠”,确保用户的输入在用它去查询以前已经是安 ...