Algorithm-动态规划
文章平均质量分 73
xiaozhuaixifu
这个作者很懒,什么都没留下…
展开
-
最长递减子序列 动态规划
这种问题很广泛,啥也不说,直接看代码:#include #include using namespace std;int num[100]; //记录序列的数组 int dp[100];int main(){ int n; while(cin>>n) { for(int i=1;i<=n;i++) cin>>num[i]; memset(dp,0原创 2013-05-23 12:46:20 · 842 阅读 · 0 评论 -
poj 3176 Cow Bowling (DP)
http://poj.org/problem?id=3176水的数字三角形,DP转移方程: dp[i][j]=max(dp[i-1][j-1],dp[i-1][j])+num[i][j].(j>1); dp[i][j]=dp[i-1][j]+num[i][j] (j==1)在最后一行原创 2013-08-30 14:04:59 · 881 阅读 · 0 评论 -
poj 2229 Sumsets (DP)
http://poj.org/problem?id=2229DescriptionFarmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an integer power原创 2013-08-30 20:27:41 · 1127 阅读 · 0 评论 -
poj 1631 Bridging signals (LIS 最长递增子序列 DP-二分)
题目:http://poj.org/problem?id=1631思路:LIS 最长递增子序列,如果用一般的动态规划算法,复杂度是O(n^2),题目的数据规模下会超时,采用二分的思想:复杂度是O(nlogn)代码:首先是一般的DP: #include#include#includeusing namespace std;const int MAX=40001;int dp原创 2013-09-06 13:14:43 · 1461 阅读 · 4 评论 -
poj 3616 Milking Time ---DP(带权重的区间动态规划)
题目:http://poj.org/problem?id=3616这题就是一个小小变形的带权重的任务调度问题 --interval scheduling--思路:首先按照每个区间的结束时间排序,再进行预处理:算出与每个区间相互兼容的最大区间下标保存在P数组里。状态:dp[i]=max{ dp[i-1],dp[p[i]]+w[i] } 代码:1A#include#include原创 2013-09-01 11:13:17 · 2503 阅读 · 0 评论 -
poj 3280 Cheapest Palindrome ---(DP 回文串)
题目链接:http://poj.org/problem?id=3280思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用。dp[i][j]=min(dp[i+1][j]+cost[s[i-1]-'a'],dp[i][j-1]+cost[s[j-1]-'a']);if(s[i-1]==s[j-1]) dp[i][j]=min(dp[i+1][j-1],dp[i]原创 2013-09-01 17:12:57 · 1088 阅读 · 0 评论 -
poj 2385 Apple Catching DP
题目链接:http://poj.org/problem?id=2385状态定义: dp[i][j] :=前i秒,移动j次接到的最大苹果数量。状态转移: dp[i][j]=dp[i-1][j]+num[0][i]; (j==0) dp[i][j]=max{dp[i-1][j-1],dp[i-1][j]}+num[j%2==1][i]; (1=这里输原创 2013-09-01 10:21:46 · 2030 阅读 · 1 评论 -
九度OJ 题目1529:棋盘寻宝 (简单DP)
题目:http://ac.jobdu.com/problem.php?pid=1529简单DP:#include#include#include#includeusing namespace std;int dp[9][9];int num[9][9];int main(){ //ifstream fin; //fin.open("input.txt"); while原创 2013-09-07 22:14:48 · 1185 阅读 · 0 评论 -
poj 1742 Coins 多重背包变形
题目:http://poj.org/problem?id=1742用bool dp[i]来表示价格i是否能被表示出。直接做:#include#include#include#includeusing namespace std;const int MAX_N=100001;bool dp[MAX_N];int num[105],val[105];int main(){原创 2013-09-02 13:54:47 · 2046 阅读 · 0 评论 -
poj 3046 Ant Counting (DP多重背包变形)
题目:http://poj.org/problem?id=3046思路: dp [i] [j] :=前i种 构成个数为j的方法数。#include #include #include int T,A,S,B;int hash[1010];int dp[1010][10100];const int MOD=1e6;using namespace std;int main()原创 2013-09-02 22:20:01 · 1458 阅读 · 0 评论 -
poj 3181 Dollar Dayz (整数划分问题---递归+DP)
题目:http://poj.org/problem?id=3181思路:将整数N划分为一系列正整数之和,最大不超过K。称为整数N的K划分。递归:直接看代码:动态规划:dp[i][j]:=将整数i做j划分的方法数。 dp[i][j]=dp[i][i]; if(j>i) dp[i][j]=dp[i-j][j]+dp[i][j-1];//分j出现不出现两种情况 d原创 2013-09-03 22:29:51 · 1543 阅读 · 0 评论 -
poj 3254 Corn Fields (状态压缩DP)
题目:http://poj.org/problem?id=3254思路见代码:#includeusing namespace std;const int MOD=100000000;const int MAX_N_M=12;int row_state[MAX_N_M];// 枚举出合法的状态数 int LegalState[2000],nstate;int dp[MAX原创 2013-09-20 21:54:41 · 1218 阅读 · 0 评论 -
九度OJ 9月赛第二场 题目1542:黑白迷阵 (状态压缩DP)
题目描述:黑白迷阵是一个GrassLand编写的手机游戏,它的规则非常简单,有如下4*5的棋盘,其中一些是格子是黑色,一些格子是白色的,每当点击其中某一个格子,它以及它上下左右五个格子的颜色会发出反转,如下图 游戏胜利的条件很简单,把所有的格子变为黑色即可。GrassLand想知道,给定一原创 2013-09-22 22:21:58 · 1696 阅读 · 0 评论 -
【LeetCode】Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the原创 2013-10-13 17:30:36 · 1668 阅读 · 0 评论 -
【LeetCode】Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at原创 2013-10-13 20:41:06 · 934 阅读 · 0 评论 -
【字符串动态规划】最长回文字串+交替字符串
Interleaving StringTotal Accepted: 7740 Total Submissions: 41615 Given s1, s2, s3, find whether s3 is formed by the interleaving ofs1 and s2.For example,Given:s1 = "aabcc",s2 = "dbbca"转载 2014-05-03 10:40:29 · 1481 阅读 · 0 评论 -
poj 2411 Mondriaan's Dream 状态压缩DP
http://blog.csdn.net/hopeztm/article/details/8062562这篇博文比较容易懂。题目这个题目的题意很容易理解,在一个N*M的格子里,我们现在有两种类型的 砖块,1 * 2 和 2 * 1,问一共有多少种方案,可以将整个N*M的空间都填满。最简单的例子就是下面的了:编程之美中题目:某年夏天,位于希转载 2013-08-23 14:15:29 · 2212 阅读 · 0 评论 -
部分和问题---多重部分和问题---动态规划
第一:01部分和(每个数只取一次)给定整数a1、a2、.......an,判断是否可以从中选出若干数,使它们的和恰好为K。输入:n=4a={1,2,4,7}k=13输出:Yes(13=2+4+7)思路:先介绍一种深度优先的搜索方法,从a1顺序决定每个数加或不加,在全部n个数都决定之后在判断和是否为K即可。这个搜索的复杂度是O(2^n).int a[MAXN];原创 2013-08-04 10:30:16 · 2528 阅读 · 1 评论 -
poj 2686 Traveling by Stagecoach ---状态压缩DP
题意:给出一个简单带权无向图和起止点,以及若干张马车车票,每张车票可以雇到相应数量的马。点 u, v 间有边时,从 u 到 v 或从 v 到 u 必须用且仅用一张车票,花费的时间为 w(u, v) / ticket[i],其中 w(u, v) 表示边的权值,ticket[i] 表示第 i 张车票可以雇到的马匹数。求从起点到终点花费的最小时间。如果不能到达终点,输出“Impossible原创 2013-08-21 22:15:18 · 1193 阅读 · 0 评论 -
求连续子数组的最大和 庞果在线编程
一个整型数组,数组里有正数也有负数。 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和,求所有子数组的和的最大值,要求时间复杂度为O(n)。 例如输入的数组为1, -2, 3, 10, -4, 7, 2, -5,那么最大的子数组为3, 10, -4, 7, 2,因此输出为该子数组的和18, 请完成函数int MaxSum(int* a,int n)。#includ原创 2013-05-23 12:40:30 · 746 阅读 · 0 评论 -
HDOJ 1284
钱币兑换问题在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法。请你编程序计算出共有多少种兑法。Sample Input293412553Sample Output71883113137761思路:转化为完全背包来求解,主意第二重循环里面的遍历书序是顺序遍历。如果是01背包则逆序遍历。#include #include原创 2013-05-23 17:16:25 · 616 阅读 · 0 评论 -
HDOJ 2191 多重背包问题
下面是用最基本的转化方法将其转化为01背包问题,未优化的。题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2191#include using namespace std;int main(){ int nCase,Limit,nKind,i,j,k, price[101],weight[101],bag[101],dp[101]转载 2013-05-27 00:21:55 · 908 阅读 · 0 评论 -
完美未来之星编程比赛 复赛第二场 发放激活码
题目描述如从一组不重复的数字中获得随机几个数字组成验证码,其中只要是这些数字相加的和为相同的那么就认为是同一组验证码,最后希望获得一共有多少组有效验证码。现在希望获得从一组N不同的数字中,获得和为C 的组个的个数。组合的个数有可能是1个 有可能是N个 。输入格式第一行输入1个整数M,表示总得数据组数。第二行输入1个整数 2 第三行输入1个整数 C ,表示数字的和第四行输原创 2013-05-23 00:05:41 · 649 阅读 · 0 评论 -
HDOJ 2391 Filthy Rich dp动态规划.....so easy......
check the pro:http://acm.hdu.edu.cn/showproblem.php?pid=2391see the code:#include#include#include #include #include#include#include#includeusing namespace std;const int maxn=1001;int va[原创 2013-06-19 14:33:33 · 701 阅读 · 0 评论 -
2013腾讯编程马拉松初赛第〇场(3月20日) 吉哥系列故事——临时工计划---带权重的区间规划
带权重的区间规划---动态规划原创 2013-07-28 14:23:05 · 819 阅读 · 0 评论 -
POJ 2976 Dropping tests 01分数规划
第一次做这类型的题目,纪念一下。题目链接:http://poj.org/problem?id=2976这是入门级别的01分数规划问题,使用的是二分搜索,不过感觉效率还是不高,试着用Dinkelbach算法优化下。#include #include #include #include #include using namespace std;const int maxn=1转载 2013-07-23 23:06:19 · 736 阅读 · 0 评论 -
poj Dropping tests 01分数规划---Dinkelbach算法
果然比二分要快将近一倍。63MS。二分94MS。#include #include #include #include #include #include using namespace std;const int maxn=1005;double a[maxn],b[maxn];const double eps=1e-6;int n,k;struct dnode{原创 2013-07-23 23:57:33 · 1244 阅读 · 0 评论 -
hdoj 3555 Bomb/ hdoj 3652 B-number 数位DP 初解禁!!
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3555学习下如此高端的DP;//dp[i][j]表示计算到第i位时,状态为j的数的个数//j=0表示之前不含49且前一位不是4,j=1表示之前不含49但前一位是4,j=2表示之前已经包含49//dfs函数:pos为当前所处的位,have记录状态(即dp数组的第二维),doing表转载 2013-07-31 19:49:44 · 728 阅读 · 0 评论 -
CodeForces 55D 数位统计 记忆化搜索
/* a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. 问一个区间内[l,r]有多少个Beautiful数字 范围9*10^18 数位统计问题,构造状态也挺难的,我想不出,我的思维局限在用递推去初始化状态,转载 2013-07-31 22:01:45 · 654 阅读 · 0 评论 -
poj 2728 Desert King(最优比例生成树)
#include #include #include #include #include #include using namespace std;const int maxn=1005;const double eps=1e-6;const double inf=0xffffffff;struct node{ double x,y,h;}no[maxn];int n;原创 2013-07-25 13:30:38 · 1011 阅读 · 0 评论 -
2013腾讯编程马拉松初赛第一场(3月21日) 湫湫系列故事——减肥记I hdu 4508--- 完全背包
#include#include#include#include#includeusing namespace std;int a[105],b[105];int dp[100005];int main(){ int n,m; while(cin>>n) { int cnt=1,x,y; for(int i=1;i<=n;i++) { cin>>x>>y原创 2013-08-01 20:59:59 · 776 阅读 · 0 评论 -
hdu 2089 不要62 数位dp
#include#include#include#includeusing namespace std;const int N = 1e6 ;int dp[10][2] , digit[10];/*state 标记前缀是否为6*/int dfs(int len,bool state,bool fp){ if(!len) return 1; if(转载 2013-08-01 18:18:06 · 694 阅读 · 0 评论 -
2013腾讯编程马拉松初赛第〇场(3月20日)HDOJ 4504 威威猫系列故事——篮球梦
转自:http://blog.csdn.net/aclion/article/details/8703623#include #include int A, B, t; int tA, tB, round, dp[30][100]; void init() { memset( dp, 0, sizeof(dp) ); dp[1][1] = dp[1转载 2013-07-28 20:47:44 · 899 阅读 · 0 评论 -
砝码称重问题求解---动态规划与母函数方法
转自:http://www.cnblogs.com/xudong-bupt/archive/2013/03/17/2964909.html砝码称重问题:设有1g、2g、3g、5g、10g、20g的砝码各若干枚(其质量),求出用他们能称出的质量的种类数(不包括质量为0的情况)。 一、动态规划方法求解 设dp[1000]数组为标记数组。当dp[i]=0时,表示质量为i的转载 2013-07-28 21:53:46 · 2147 阅读 · 0 评论 -
【HackerRank】Angry Children 2 排序+动态规划
Bill Gates is on one of his philanthropic journeys to a village in Utopia. He hasN packets of candies and would like to distribute one packet to each of theK children in the village (each packet m转载 2014-04-07 15:57:04 · 2099 阅读 · 0 评论