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 ...
随机推荐
- php 通过referer防盗链(以图片为例)
1.在网页里访问站外图片时,服务器如何知道是在站外引用的呢? (1)对比本服务器请求与跨服务器请求 图一——本服务器请求 图二——显示盗链的referer信息 通过对比也就知道referer显示的是引 ...
- shopnc b2b2c如何开启伪静态??
shopnc b2b2c开启伪静态的方法 一. windows环境下 1.先下载isapi rewrite插件,安装,然后我们把根目录下面的htaccess.txt那么修改成.htaccess即可. ...
- PHP-语法(www.w3school.com.cn/php)
写在前面: 假设系统里已安装PHP软件 PHP是一种脚本语言,执行PHP脚本后向浏览器返回纯HTML语言(即后台将.php文件的执行结果以纯HTML的形式返回到前端) ---------------- ...
- 导入Excel加行公式和验证
package com.sprucetec.tms.controller.fee.export; import com.sprucetec.tms.controller.base.BaseFeeExp ...
- c++ bitset使用
A bitset is a special container class that is designed to store bits (elements with only two possibl ...
- linux 中多线程使用
/* * 对于线程之间的操作: * 一个进程中会有多个线程,各个线程之间的或向切换会付出很小的代价相比于进程之间的切换 * 为什么要引入多线程: * 1.与进程相比,他是一种非常节俭的多任务的操作方式 ...
- Thrift对多接口服务的支持
Thrift对多接口服务的支持 Thrift在0.9.1版本之前,一直只提交了对单一接口服务的支持,即一个RPC服务器(对应一个端口)支持一个服务接口的实现. 但是很多时候,我们的服务不能实现在一个接 ...
- FreeCodecamp:Repeat a string repeat a string
要求: 重要的事情说3遍! 重复一个指定的字符串 num次,如果num是一个负数则返回一个空字符串. 结果: repeat("*", 3) 应该返回"***". ...
- C指针陷阱
问题一: #include <stdio.h> int main(int argc, char *argv[]) { ]={ ,,,, }; ); printf(),*(p-)); ; } ...
- 建立dblink,clob
建立dblink的方法, 如果有个测试库A,要访问生产库里的数据,那么可以直接在测试库A里建立一个dblink,然后数据库A就可以直接访问测试库B的数据了. -- 删除已有的dblink drop d ...