随笔分类 -  动态规划

摘要:给定各种数量的硬币,求能够组合成多少种符合题意的组合数。代码如下:#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <iostream>#define MAXN 100000using namespace std;int N, M, dp[MAXN+5], p[105], n[105];void zobag(int x, int k){ int ans = 0; if (M>=k*p[x]) { for (int i = M 阅读全文
posted @ 2012-04-23 15:08 沐阳 阅读(291) 评论(0) 推荐(0)
摘要:题义:给定一个不长于100的字符串,求输入完整个串的最少按键次数。思路:对于打完每一个字符后,保留其保留大写锁定和非大写的两种状态的最少按键次数即可,做题中竟然忘了在大写锁定的时候可以shift+alphabet可以打出小写。代码如下:#include <cstdlib>#include <cstring>#include <cstdio>#include <cctype>#include <algorithm>using namespace std;int dp[105][2]; // 零表示以小写结尾,1代表以大写结尾char s 阅读全文
posted @ 2012-04-21 09:45 沐阳 阅读(225) 评论(0) 推荐(0)
摘要:这题是一个二维背包的题目,刚开始并没有那样去做,只开了一维的空间来存储状态,结果很多的数据都没有跑过去。其实这题这样来问的话可能就明了很多了,求在指定的容忍值和指定的杀怪数下,求最大能够得到了经验数,可能我们马上就能想到二维背包,一维为杀怪数,二维为容忍值,在做一个完全背包,可惜这题问的是在满足升级情况下最大的容忍值,这其实也能够用上面构建的模型了解决。只要的求解的过程每次将得到的经验值与给定的N进行比较,如果超过了N,那么说明当前的方案能够触发升级,再将此时剩余的容忍值传递出来就可以了。总而言之,题目对攻击次数和容忍值进行了限制,那么我们就要对限制的量进行动态规划。代码如下:#include 阅读全文
posted @ 2012-04-21 08:06 沐阳 阅读(484) 评论(0) 推荐(0)
摘要:博客里面还有以前写的解题报告,表示没看懂。这里直接计算其没有被录取的概率,每次去小的值即可代码如下:#include <cstring>#include <cstdio>#include <cstdlib>#define MAXN 1005using namespace std;int N, M, s[MAXN];double p[MAXN], dp[10005];inline double min(double x, double y){ return x < y ? x : y;}void zobag(int x){ for (int i = N; 阅读全文
posted @ 2012-04-19 17:24 沐阳 阅读(294) 评论(0) 推荐(0)
摘要:#include <cstdlib>#include <cstring>#include <cstdio>#define MAXN 105using namespace std;int N, seq[MAXN][MAXN], dp[MAXN][MAXN];inline int max(int x, int y){ return x > y ? x : y;}int DP(){ for (int i = 1; i <= N; ++i) { dp[N][i] = seq[N][i]; } for (int i = N-1; i >= 1; -- 阅读全文
posted @ 2012-04-19 17:00 沐阳 阅读(299) 评论(0) 推荐(0)
摘要:通过给定的数据组成可及的状态。最后再中中间开始遍历所有可及的状态。就是一个完全背包,用二进制优化。代码如下:#include <cstdlib>#include <cstdio>#include <cstring>#define MAXN 250005using namespace std;int N, dp[MAXN], p[55], n[55], total;void zobag(int x) { for (int i = total>>1; i >= x; --i) { if (dp[i-x]) { dp[i] = 1; } ... 阅读全文
posted @ 2012-04-19 16:45 沐阳 阅读(280) 评论(0) 推荐(0)
摘要:简单动态规划,将长方体分解成六个,然后二重排序后,DP即可。对于当前方块,选择前面的能够放置的方块的最大值加上本身的高度。代码如下:#include <cstdlib>#include <cstring>#include <cstdio>#include <algorithm>#define MAXN 200using namespace std;// 对于每个长方体,有三种不同的方法。int N, dp[MAXN];struct Node{ int x, y, z;}e[MAXN];inline int max(int x, int y){ r 阅读全文
posted @ 2012-04-18 22:42 沐阳 阅读(273) 评论(0) 推荐(0)
摘要:01背包最裸模板题#include <cstdlib>#include <cstring>#include <cstdio>#define MAXN 1005using namespace std;int N, V, p[MAXN], v[MAXN], dp[MAXN];inline int max(int x, int y){ return x > y ? x : y;}void zobag(int x){ for (int i = V; i >= v[x]; --i) { dp[i] = max(dp[i], dp[i-v[x]]+p[x]) 阅读全文
posted @ 2012-04-18 17:11 沐阳 阅读(167) 评论(0) 推荐(0)
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1505自己写的屌丝代码:#include <cstdlib>#include <cstring>#include <cstdio>#define MAXN 1050using namespace std;int N, M, u[MAXN][MAXN], l[MAXN][MAXN], G[MAXN][MAXN];inline int max(int x, int y){ return x > y ? x : y;}inline int min(int x, int y) 阅读全文
posted @ 2012-04-18 16:37 沐阳 阅读(602) 评论(0) 推荐(0)
摘要:先粘上TLE的代码,先对高度离散化,然后DP高度求解。复杂度过高。代码如下:View Code #include <cstdlib>#include <cstdio>#include <cstring>#include <map>#include <iostream>#include <algorithm>#define MAXN 100005using namespace std; int N, cnt;int seq[MAXN], cseq[MAXN];long long dp[MAXN]; map<int,in 阅读全文
posted @ 2012-04-17 21:07 沐阳 阅读(407) 评论(0) 推荐(0)
摘要:这次看了这题的动态规划写法,顿时觉得好理解多了。这里要对dp[i]的意义进行一下说明,dp[i]表示从1~i包含第i个数的最大子串和,如果前i-1个数的包含i-1在内的最大和为正数的话,那么包含第i个数的最大子串和就是dp[i-1]+seq[i],否则dp[i] 就等于seq[i]了。该题的动态递归写法并没有直接给出最终答案的状态,但是却能够根据另外的状态推到出答案。这点值得我们思考。代码如下:#include <cstring>#include <cstdlib>#include <cstdio>#include <algorithm>#def 阅读全文
posted @ 2012-04-17 17:04 沐阳 阅读(433) 评论(0) 推荐(0)
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1864这一题的题义真的让人很伤神啊。其中不超过600元,是指某一类物品的总和不超过600元,1000元是指整张发票的钱不超过1000元,对于浮点数,我们将其统一乘以100。该题背包的体积就是发票数,对于第 i 张发票,其最大报销额就是 dp[i] = Max[ ( dp[j]+Fee[i] ) <= LIMIT], 其中dp[j]可达(满足在1~i-1张发票之前能够报销j张发票,其总和为不超过额定最大报销额的最大值)。代码如下:#include <cstdlib>#include <c 阅读全文
posted @ 2012-04-17 15:47 沐阳 阅读(287) 评论(0) 推荐(0)
摘要:题目就是概率的问题,意思是去抢A银行,被捉的概率是ai,抢B银行,被捉的概率是bi,因此不被捉的概率是(1-ai)*(1-bi)......因此整个题目我们用逃避概率来计算。代码如下:#include <cstdio>#include <cstring>#include <cstdlib> using namespace std;struct Node{ int m; double poss;}e[105];double dp[10005];int N, max;double poss;inline double Max(double x, double y 阅读全文
posted @ 2012-04-10 22:17 沐阳 阅读(245) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=2533该题是最裸的LIS题,这里有两种方法。代码如下:#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int a[10000], dp[10004];int main(){ int N; while (scanf("%d", &N) == 1) { int ans = 1, m; for (int i = 0; i < N; ++i) { scanf("%d&q 阅读全文
posted @ 2012-03-03 16:25 沐阳 阅读(278) 评论(0) 推荐(0)
摘要:该题题义是求一个序列中非递减的子序列的个数,其实就是一个求逆序对的题,这里当然就是用树状数组来解决了。首先对输入数据进行离散化,以便于在树状数组上面工作,然后利用DP公式计算ans[i] = sum{ ans[j], j < i },可以理解为在前面的所有满足要求的集合上加上这个较大的数。参看http://www.cppblog.com/menjitianya/archive/2011/04/06/143510.aspx代码如下:#include <cstring>#include <cstdio>#include <algorithm>#includ 阅读全文
posted @ 2012-02-19 10:30 沐阳 阅读(391) 评论(0) 推荐(0)
摘要:Exchange RatesTime Limit: 1000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 157Accepted Submission(s): 89Problem DescriptionNow that the Loonie is hovering about par with the Greenback, you have decided to use your $1000 entrance scholarship to engage in currenc 阅读全文
posted @ 2011-09-02 16:56 沐阳 阅读(522) 评论(0) 推荐(0)
摘要:Common SubsequenceTime Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9595Accepted Submission(s): 3923Problem DescriptionA subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x 阅读全文
posted @ 2011-08-31 23:57 沐阳 阅读(5961) 评论(0) 推荐(1)
摘要:Free DIY TourTime Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1135Accepted Submission(s): 381Problem DescriptionWeiwei is a software engineer of ShiningSoft. He has just excellently fulfilled a software project with his fellow workers. His boss is.. 阅读全文
posted @ 2011-08-31 22:51 沐阳 阅读(682) 评论(0) 推荐(0)
摘要:Super Jumping! Jumping! Jumping!Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9547Accepted Submission(s): 3918Problem DescriptionNowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good 阅读全文
posted @ 2011-08-31 20:57 沐阳 阅读(412) 评论(0) 推荐(0)
摘要:Distribute MessageTime Limit: 1000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 605Accepted Submission(s): 259Problem DescriptionThe contest’s message distribution is a big thing in prepare. Assuming N students stand in a row, from the row-head start transmit me 阅读全文
posted @ 2011-08-27 17:26 沐阳 阅读(434) 评论(0) 推荐(0)