![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
动态规划
文章平均质量分 66
闻道-问道
这个作者很懒,什么都没留下…
展开
-
动态规划总结
动态规划是一种解决最优化问题的方法,动态规划最大的特点是变化多端,解题方法不是按照一定套路来的。动态规划一般是能分阶段的问题,可以把大问题转化为小问题,充分利用计算机重复处理问题的特点,把这个问题解决掉。并且动态规划中的变量都是有一定含义的,每个变量都有它所包含的意思,比如:在最长上升子序列这个题中,dp[n]代表了以第n个数结尾的最长子序列的长度。最重要的是动态规划问题要有状态转移方程,这个方程...原创 2017-04-17 20:23:34 · 310 阅读 · 0 评论 -
动态规划--开餐馆
题目大概:一条路上可以在n个位置建饭店,每个饭店到最左边的距离不同,盈利不同,并且在k距离内只能有一个饭店。思路:这个题很像背包问题。dp[n]是前n个饭店的最大盈利,a[n],位置,b[n]盈利,c[n]n到最左边的距离。1。。当前位置不建,dp[n]=dp[n-1]。2。。当前位置建,dp[n]=max(dp[k] k=1....n-1 )+b[n]。感想:原创 2017-04-16 17:29:33 · 1976 阅读 · 0 评论 -
动态规划--滑雪
题目大概:有一个矩形滑雪场,只能从高处滑到低的地方,可以向四个方向滑,问这个滑雪场最长的一条滑雪道是多长。思路:dp题。a[i][j]为矩阵各个地方的高度。dp[i][j]为滑到改点处的最长距离。滑到一个点可以从它的四个方向滑过去,dp[i][j]=max(dp[i-1][j]+1,dp[i][j-1]+1,dp[i+1][j-1]+1,dp[i-1][j+1]+1)。原创 2017-04-12 21:20:07 · 673 阅读 · 0 评论 -
动态规划--最大上升子序列和
题目大概:求一行数的最大上升子序列和。思路:与求最长上升子序列长度差不多,都是先求上升子序列,不过,这个题是把每次加1,改为了求和,然后比较一下,求出最大的即可。感想:有时候要学会变通。代码:#include using namespace std;int main(){int n,ma=0,sum=0;int a[1001]={0},b[1001]={原创 2017-04-12 20:46:41 · 960 阅读 · 0 评论 -
动态规划--怪盗基德的滑翔翼
题目大概:给出一行数,从任意一个数出发,可以向两个方向走,但数字大小依次减小,求最长的一条路。思路:这个题一看和 登山 这道题差不多。都是任选一个数,求两个方向最长上升子序列。不过,这个题不是相加,而是取最大值。最后,再寻找一下所有数的最长上升子序列的最大的一个。感想:好多题思路类似。代码:#include #include using na原创 2017-04-12 21:06:26 · 644 阅读 · 0 评论 -
动态规划--计算字符串距离
题目大概:给出两个字符串,可以对字符串进行替换,删除,增添,使得这两个字符串一样,问最少需要几步。思路:dp题。刚读完题,感觉这个题好熟悉。两数组为a[i],,,b[i]。dp[i][j]记录a数组i长度,b数组j长度是的最少步骤。1.....当a[i]==b[j]时,dp[i][j]==dp[i-1][j-1]2.....当不相等时,dp[i][j]=min(dp原创 2017-04-12 20:58:10 · 507 阅读 · 0 评论 -
动态规划--合唱队形
题目大概:一行数字要求从两边到中间依次增大,问给出的一行数字要去掉多少才能形成这种数列。思路:可以任选一个做中间数k,从1到n。然后求第一个数到k的最长上升子序列的长度la,再求最后一个数到k的最长上升子序列的长度lb。最后n-(la+la-1)就是去掉的数字个数,减一是因为多算了一个k。感想:有些题需要几个简单的算法混合使用或者重复使用。代码:#inclu原创 2017-04-12 20:41:11 · 559 阅读 · 0 评论 -
动态规划--三角形最佳路径
题目大概:有一个直角三角形,每个位置都有一个数字,从顶部走到最低端的数字之和最大是多少(只能向下或者斜下走)。思路:dp题。a[i][j]是第i行第j个数的最大值。从最底部向上推,a[i][j]=min(a[i+1][j]+h[i][j],a[i+1][j+1]+h[i][j]));感想:无。代码: #include <iostream>#i...原创 2017-04-12 20:33:33 · 624 阅读 · 0 评论 -
动态规划--买书(背包方案数)
题目大概:用n元买书,书有10 20 50 100元的。问有多少种买书方案。思路:这个题是完全背包问题的方案数问题,即每一样物品是可以无限制的拿取的。状态h[i]是i元的方案数,这个题和数字组合非常相似。把完全背包问题变换一下就可以了。即把完全背包的两层循环里的max(h[i],h[i-a[j]])改成h[i]+h[i-a[j]]。感想:无。代码:已整理...原创 2017-04-05 20:26:43 · 1211 阅读 · 0 评论 -
动态规划--数字组合(背包方案数)
题目大概:输入一个数字t, 输入n个数子,用这n个数有多少种组合成数字t的方案(每个数字只能用一次)。思路:读完题后,感觉这个题是背包的方案数问题。并且这个题是一个物品只能用一次的背包,即01背包问题变化的方案数问题。状态:a[i]表示用这些数字组成数字 i 所有的方案数。一直从1到t。然后只需要把01背包的思路改动一下,就可以完成这个题了。01背包第一层循环int y=...原创 2017-04-05 18:55:16 · 4095 阅读 · 0 评论 -
动态规划--移动路线
题目大概:有一个m行n列的矩阵,最左下角是(1,1)从这个方格走到最右上角有多少种走法。.思路:用递归来实现,一般从任意一个方格开始 有两种走法,向右走和向左走。于是递归函数里也分为两条路。递归的终止条件是当走到了最右上角,走法就会加一条。递归限制条件,行不能超过m列不能超过n。感想:dp和递归递推确实是一类题。代码:#include #include#原创 2017-04-01 12:40:10 · 632 阅读 · 0 评论 -
动态规划--登山
题目大概:给出一些数字,必须按照给出的顺序,找出从最小的数到最大的数,再到最小的数的最长路径。思路:刚开始读完题 以为是最长上升子序列问题,直接复制代码,样例通过,但没ac,再读一遍题后发现,理解错题意了。这个题和最长上升子序列问题有一定联系,就像老师上课讲的 最大字段和问题 与 最大二字段和的问题 的联系差不多。那就好办了,找一个最高峰,算出它前面的最长上升子序列原创 2017-03-30 11:41:53 · 452 阅读 · 0 评论 -
动态规划--吃糖果
题目大概:每天可以吃一块或者两块糖果,给出n块糖果,问可以有几种吃法。思路:根据题意,当吃到第n块糖的时候,可以是吃1块吃到第n块,也可以吃2块。所以当吃到第n块的时候,他的方法数是吃n-1和n-2块糖的方法数之和。因此。状态:a[n]是吃第n块糖的时候的方法数。子问题:第n块糖,最后是那种方法吃到的。有两种。状态转移方程:第n块糖有两种情况。原创 2017-03-30 11:28:36 · 624 阅读 · 0 评论 -
动态规划--公共子序列
题目大概:输入两个字符串,输出其中最长的公共子序列。思路:状态:有两个字符串,就要有两个控制状态的变量a[i][j]。第一个字符串的第i个字符,第二个字符串的第j个字符。b[i]是第一个字符串,c[i]是第二个字符串。子问题:求第一个字符串的第i个字符和第二个字符串的第j个字符是不是相等。状态转移方程:第一个字符串的第i个字符和第二个字符串的第j个字符有两种情况。原创 2017-03-30 11:07:42 · 254 阅读 · 0 评论 -
动态规划--采药(01背包)
总时间限制: 1000ms内存限制: 65536kB描述辰辰是个很有潜能、天资聪颖的孩子,他的梦想是称为世界上最伟大的医师。为此,他想拜附近最有威望的医师为师。医师为了判断他的资质,给他出了一个难题。医师把他带到个到处都是草药的山洞里对他说:“孩子,这个山洞里有一些不同的草药,采每一株都需要一些时间,每一株也有它自身的价值。我会给你一段时间,在这段时间里,你可以采到一些草药。如果你...原创 2017-03-28 22:36:24 · 706 阅读 · 0 评论 -
动态规划--最大子矩阵
总时间限制: 1000ms 内存限制: 65536kB描述已知矩阵的大小定义为矩阵中所有元素的和。给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵。比如,如下4 * 4的矩阵0 -2 -7 09 2 -6 2-4 1 -4 1-1 8 0 -2的最大子矩阵是9 2-4 1-1 8这个子矩阵的大小原创 2017-03-28 22:12:39 · 481 阅读 · 0 评论 -
动态规划--最长上升子序列
总时间限制:2000ms内存限制:65536kB描述一个数的序列bi,当b1 <b2 bS的时候,我们称这个序列是上升的。对于给定的一个序列(a1,a2, ..., aN),我们可以得到一些上升的子序列(ai1,ai2, ...,aiK),这里1 <=i1 i2 < ... <iK 你的任务,就是对于给定的序列,求出最长上升子序列的长度。原创 2017-03-28 21:53:50 · 298 阅读 · 0 评论 -
动态规划--最低通行费
题目大概:有一个矩形n*n,每个点处都有一个值,从左上角走到右下角,值最小是多少,走的步数小于2*n-1,可以向四个方向走。思路:a[n][m]表示到这个格子为止的最小值。b[n][m]是矩形中的数这个题说有四个方向,其实算一算步数,只能向右向下走。倒着推,a[n][m]的最小值是a[n-1][m]和a[n][m-1]的最小值加上a[n][m]上的原来的数b[n][m]。感想:原创 2017-04-16 17:06:27 · 322 阅读 · 0 评论 -
动态规划--大盗阿福
题目大概:阿福只能偷不相邻的两个商店的钱,共有n个商店,问阿福最多能偷多少钱。思路:dp[n]表示前n个商店最多偷的钱数。a[n]表示每个商店的钱数。1。。。当前商店如果被偷,则dp[n]=dp[n-2]+a[n].2。。。不被偷,则dp[n]=dp[n-1]。感想:一个递推题。代码:#include #include #include using n原创 2017-04-16 17:13:02 · 467 阅读 · 0 评论 -
动态规划--Maximum sum
题目大概:计算如下图所示的d(A)。t1 t2d(A) = max{ ∑ai + ∑aj | 1 i=s1 j=s2思路:这个题其实是求最大连续2字段和问题。就是从前面慢慢加。就是求两段最大的不重合的字段加起来就行了。原创 2017-04-16 17:45:05 · 347 阅读 · 0 评论 -
动态规划--踩方格
题目大概:在一个无限大的矩形中,可以向上或者向左,右走。但走过的地方不能重复走,不能向下走。问走n步有几种走法。思路:dp[n]为走了n步的方法数。r[n]为向右走,l[n]是向左走,o[n]是向上走。dp[n]=o[n-1]+r[n-1]+l[n-1].o[n]=r[n-1]+l[n-1]+o[n-1]。向上的前一步可以是前一步的三种情况。r[n]=r[n-1]+o[n-原创 2017-04-16 18:29:29 · 857 阅读 · 1 评论 -
动态规划--鸣人的影分身
题目大概:鸣人的m个查克拉分到n个分身中,有多少种分法。思路:dp[m][n]表示前m个查克拉分到前n个分身的方法数。.1。。。第n个分身不用查克拉,dp[m][n]=dp[m][n-1]。2。。。用k个查克拉。dp[m][n]=dp[m-k][n].。感想:刚开始这个题竟然想错了qaq。代码:#include #include using namesp原创 2017-04-16 17:19:35 · 726 阅读 · 0 评论 -
1002(dp+树状数组+离散化)
Problem DescriptionHow many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3, ...., sn} ? For example, we assume that S = {1, 2, 3}, and you can find seven nondecreasing subse原创 2017-08-23 19:45:22 · 259 阅读 · 0 评论 -
nlogn的最长上升子序列的算法(LIS)
Problem DescriptionJGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines.Half of these cities are rich in resource (we call them rich原创 2017-10-08 21:44:29 · 251 阅读 · 0 评论 -
日记(周中)
这几周要从数论和dp中选择一个学习,由于队友选择了数论,我就选择了dp,刚开始要学习数位dp。 通过看了很多博客,知道了数位dp就是计数用的,最基础题型是计算一个区间(a,b)符合某个条件的数的个数。一般这种题就是枚举做,而数位dp就是一种暴力枚举的方法。它就是在数位上一位一位的枚举,使得这种枚举方式符合dp的定义,并且使用记忆化搜索,就会节省枚举的时间。 一般这种题都有模板。原创 2017-10-19 22:15:47 · 183 阅读 · 0 评论 -
H - Bomb
The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the time bomb. The number sequence of the time bomb counts from 1 to N. If the current number sequence inc原创 2017-10-29 22:31:04 · 265 阅读 · 0 评论 -
G - B-number
A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string "13" and can be divided by 13. For example, 130 and 2613 are wqb-numbers, but 143 and 2639 ar原创 2017-10-29 22:27:17 · 273 阅读 · 0 评论 -
M - windy数
windy定义了一种windy数。不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。 windy想知道,在A和B之间,包括A和B,总共有多少个windy数?Input 包含两个整数,A B。Output 一个整数Sample Input【输入样例一】1 10【输入样例二】25 50Sample Output【输出样例一】9【输出样例二原创 2017-10-29 22:33:56 · 286 阅读 · 0 评论 -
Amount of Degrees (数位dp)
Create a code to determine the amount of integers, lying in the set [ X;Y] and being a sum of exactly K different integer degrees of B. Example. Let X=15, Y=20,K=2, B=2. By this example 3 nu原创 2017-11-09 21:00:36 · 1518 阅读 · 0 评论 -
Beautiful numbers (数位dp)
Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number isbeautiful if and only if it is divisible by each of its nonzero digits. We will not argue wit原创 2017-11-09 21:25:54 · 1488 阅读 · 0 评论 -
Apocalypse Someday (数位dp+二分)
The number 666 is considered to be the occult “number of the beast” and is a well used number in all major apocalypse themed blockbuster movies. However the number 666 can’t always be used in the scri原创 2017-11-09 21:13:00 · 313 阅读 · 0 评论 -
Palindromic Numbers (数位dp)
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the same when its digits are reversed. In this problem you will be given two integersi j, you have to fin原创 2017-11-09 21:42:29 · 351 阅读 · 0 评论 -
count 数字计数 (数位dp)
给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次。Input输入文件中仅包含一行两个整数a、b,含义如上所述。Output输出文件中包含一行10个整数,分别表示0-9在[a,b]中出现了多少次。Sample Input1 99Sample Output9 20 20 20 20 20 20 20 20 20Hint30%的数据中,a原创 2017-11-09 21:56:24 · 1858 阅读 · 0 评论 -
Investigation (数位dp)
An integer is divisible by 3 if the sum of its digits is also divisible by 3. For example, 3702 is divisible by 3 and 12 (3+7+0+2) is also divisible by 3. This property also holds for the integer 9.原创 2017-11-01 23:52:32 · 331 阅读 · 0 评论 -
K-th Nya Number (数位dp+二分)
Arcueid likes nya number very much. A nya number is the number which has exactly X fours and Y sevens(If X=2 and Y=3 , 172441277 and 47770142 are nya numbers.But 14777 is not a nya number ,because i原创 2017-11-10 23:06:19 · 251 阅读 · 0 评论 -
Mirror Number (数位dp)
A number is called a Mirror number if on lateral inversion, it gives the same number i.e it looks the same in a mirror. For example 101 is a mirror number while 100 is not. Given two numbers a and b原创 2017-11-12 23:21:26 · 299 阅读 · 0 评论 -
Horse Races (数位dp)
Petya likes horse racing very much. Horses numbered from l to r take part in the races. Petya wants to evaluate the probability of victory; for some reason, to do that he needs to know the amount of原创 2017-11-12 23:41:02 · 586 阅读 · 0 评论 -
Anniversary party (树形dp)
There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a tr原创 2017-11-14 23:59:21 · 235 阅读 · 0 评论 -
Contestants Division (树形dp)
In the new ACM-ICPC Regional Contest, a special monitoring and submitting system will be set up, and students will be able to compete at their own universities. However there’s one problem. Due to the原创 2017-11-19 22:59:09 · 377 阅读 · 0 评论 -
Information Disturbing (树形dp+二分)
In the battlefield , an effective way to defeat enemies is to break their communication system.The information department told you that there are n enemy soldiers and their network which have n-1原创 2017-11-19 23:12:37 · 518 阅读 · 0 评论