随笔分类 -  动态规划--线性dp

摘要:http://poj.org/problem?id=1276第一种解法 http://blog.csdn.net/lyy289065406/article/details/6648102代码View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<stdlib.h> 5 using namespace std; 6 int c[20],w[20],num[20],cc[100010],dp[100010]; 7 int main() 8 { 阅读全文
posted @ 2013-01-29 17:06 _雨 阅读(275) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=3267对dp没研究 看了看解题报告 还是比较好推的 dp[i] = min{dp[i]-1,dp[j]+i-j-length[k]} 若从J到i包含字典中的单词View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 char word[650][30]; 7 int main() 8 { 9 int i,j,k,n, 阅读全文
posted @ 2012-12-13 17:37 _雨 阅读(149) 评论(0) 推荐(0) 编辑
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=14212A 之前在实验室写的时候排好了序 没打完 回来重打的时候忘记了排序。。View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<string.h> 4 #include<algorithm> 5 #define INF 0xffffff 6 using namespace std; 7 int w[2011]; 8 __int64 dp[2011][1011]; 9 int main()1 阅读全文
posted @ 2012-09-07 11:07 _雨 阅读(155) 评论(0) 推荐(0) 编辑
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=10877A。。。。简单的递推 没考虑负数的情况 一直乱改。。View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<string.h> 4 #define INF 0xfffffff 5 using namespace std; 6 long long dp[1011],a[1011]; 7 int main() 8 { 9 int i,j,k,n,m;10 while(scanf("%d&quo 阅读全文
posted @ 2012-09-05 20:50 _雨 阅读(178) 评论(2) 推荐(0) 编辑
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=11601A 破题敲了一个多小时 最长上升子序列和最长下降子序列合起来 并把路径保留下来 题中是可以打乱顺序去找的 先按W上升或S下降排下序 再按最升和最降做View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<string.h> 4 #include<algorithm> 5 using namespace std; 6 struct node 7 { 8 int w,s,xu; 9 }q[1 阅读全文
posted @ 2012-09-05 20:06 _雨 阅读(248) 评论(0) 推荐(0) 编辑
摘要:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2368我也不知道 这份代码算不算DP 之前写的那份长长的长长的dp死活过不了 可能太长了 bug就太多了。。这个是根据必须保留的来算哪些可以删除 哪些必须保留3种情况当前这个号的日期是‘-’号,‘+’号直接不能删除 继续循环1、 若比前一个不能删除的(pre)大 也就是它们是同一年份的 如果下一个也就是i+1 的年份比i大 也就是它三是一年份的 或者 i+1的年份比pre小或相等 当前i可以删除。2、若比pre小 不同年份 如果i+1的年份比i 阅读全文
posted @ 2012-08-29 11:34 _雨 阅读(146) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=1159这题以前见过 不会 今天做比赛又看到 知道自己不会 就没多想 后来CZ说是水题 最长公共子序列 我就开始想想到求正序和逆序的最长公共子序列 不知道对不对 就自己想了几个数据试了一下 都过了 就交了 还真蒙对了 ME了一次 又现学的滚动数组View Code 1 #include <iostream> 2 #include<cstdio> 3 using namespace std; 4 int dp[3][5001]; 5 int main() 6 { 7 int i = 0,j = 0,k,n; 8 char 阅读全文
posted @ 2012-08-18 17:51 _雨 阅读(257) 评论(0) 推荐(0) 编辑
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=3339最短路+01背包以耗油量为V 以pow为价值View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<string.h> 4 using namespace std; 5 #define INF 0x3f3f3f 6 int w[101][101],p[101],pi[101],f[10001]; 7 int main() 8 { 9 int i,j,k,n,m,t,a,b,c,v;10 scanf(& 阅读全文
posted @ 2012-08-12 23:01 _雨 阅读(176) 评论(0) 推荐(0) 编辑
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1114初始化为无穷大View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<string.h> 4 #define INF 0xfffffff 5 using namespace std; 6 int main() 7 { 8 int i,j,k,n,t,f[10001],v,p[501],w[501]; 9 scanf("%d", &t);10 while(t--)11 {12 阅读全文
posted @ 2012-08-10 16:34 _雨 阅读(185) 评论(0) 推荐(0) 编辑
摘要:http://www.cppblog.com/tanky-woo/archive/2010/07/31/121803.htmlhttp://dongxicheng.org/structure/knapsack-problems/01背包(ZeroOnePack): 有N件物品和一个容量为V的背包。(每种物品均只有一件)第i件物品的费用是c[i],价值是w[i]。求解将哪些物品装入背包可使价值总和最大。这是最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放。用子问题定义状态:即f[i][v]表示前i件物品恰放入一个容量为v的背包可以获得的最大价值。则其状态转移方程便是:f[i][v]= 阅读全文
posted @ 2012-08-10 14:57 _雨 阅读(290) 评论(0) 推荐(0) 编辑
摘要:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2080最长公共子序列状态方程if (ci==cj)dp[i][j] = dp[i-1][j-1]+1;elsedp[i][j] = max{dp[i-1][j],dp[i][j-1]};View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<string.h> 4 using namespace std; 5 int main() 6 { 7 阅读全文
posted @ 2012-08-10 14:52 _雨 阅读(244) 评论(0) 推荐(0) 编辑
摘要:看了看题 没什么想法 参考着解题报告写的状态方程 dp[i][j] = max{dp[i+1][j],dp[i+1][j+1],dp[i+1][j-1]}View Code 1 #include <stdio.h> 2 #include<string.h> 3 int dp[100001][12]; 4 int max1(int x, int y, int z) 5 { 6 int m = x; 7 if(m<y) 8 m = y; 9 if(m<z)10 m = z;11 return m;12 }13 int main()14 {15 ... 阅读全文
posted @ 2012-07-12 21:32 _雨 阅读(217) 评论(0) 推荐(0) 编辑
摘要:导弹拦截系统 随时更新dp[i]的值 满足第i个导弹的高度比第j个高的而且dp[i]<dp[j]+1 就更新dp[i]的值View Code 1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 int n, i, j, dp[10001], h[10001], min; 6 while(scanf("%d", &n)!=EOF) 7 { 8 for(i = 1 ; i <= n ; i++) 9 {10 scanf("%d" , & 阅读全文
posted @ 2012-07-07 23:00 _雨 阅读(175) 评论(0) 推荐(0) 编辑
摘要:这题实在是郁闷 检查了几个小时的错误 十几次runtime 居然是没定义成全局变量m[][]存当前最大的公共子序列m[i][j] = m[i-1][j-1] +1 (x[i] = y[i])elsem[i][j] = max(m[i-1][j],m[i][j-1]);View Code 1 #include<stdio.h> 2 #include<string.h> 3 int m[1001][1001]; 4 char c1[1001], c2[1001]; 5 int main() 6 { 7 int i,j, k1, k2; 8 while(scanf(" 阅读全文
posted @ 2012-07-06 21:09 _雨 阅读(159) 评论(0) 推荐(0) 编辑
摘要:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1570递归求解View Code 1 #include<stdio.h> 2 #include<string.h> 3 int count = 0; 4 int a[21][21]; 5 int h, w; 6 void road(int m, int n) 7 { 8 int i, j; 9 if(m == 1&&n == w)10 {11 count++;12 return ;13 ... 阅读全文
posted @ 2012-05-09 16:31 _雨 阅读(154) 评论(1) 推荐(0) 编辑
摘要:Max SumProblem DescriptionGiven a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.InputThe first line of the input contains an integer T(1<=T<=20) which means the 阅读全文
posted @ 2012-04-13 20:25 _雨 阅读(234) 评论(0) 推荐(0) 编辑
摘要:最大子序列和问题问题描述:输入一组整数,求出这组数字子序列和中最大值。也就是只要求出最大子序列的和,不必求出最大的那个序列。例如:序列:-2 11 -413 -5 -2,则最大子序列和为20。序列:-6 2 4 -7 5 3 2 -1 6 -9 10 -2,则最大子序列和为16。算法一://穷举法,复杂度O(n^3)long maxSubSum1(const vector<int>& a){ long maxSum = 0; for (int i = 0; i < a.size(); i++) { for (int j = i; j < a.size(); j+ 阅读全文
posted @ 2012-04-12 23:49 _雨 阅读(313) 评论(3) 推荐(0) 编辑
摘要:Description把M个同样的苹果放在N个 同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法。Input第一行是测试数据的数目t(0 <= t <= 20)。以下每行均包含二个整数M和N,以空格分开。1<=M,N<=10。Output对输入的每组数据M和N,用一行输出相应的K。Sample Input17 3Sample Output8递推求解如果m = 1,将一个苹果放在n个盘子里,因为511和151属于同一种方法,所以只有一种放法f(1,n) = 1;如果n = 1,将m个苹果放在1个盘子里,只有一种方 阅读全文
posted @ 2012-03-30 21:25 _雨 阅读(273) 评论(0) 推荐(0) 编辑