0x50 动态规划
文章平均质量分 68
线性dp、记忆化搜索、区间dp、数位dp、树形dp
默_silence
记得一定要努力,不然往后余生,做饭是你,洗衣是你,做家务是你……是你是你还是你
展开
-
leetcode 5. 最长回文子串(区间dp)
链接:https://leetcode-cn.com/problems/longest-palindromic-substring给你一个字符串 s,找到 s 中最长的回文子串。示例 1:输入:s = “babad”输出:“bab”解释:“aba” 同样是符合题意的答案。区间dp的做法:1、先设置好动态规划数组所表示的状态dp[i][j]:表示区间 [i, j] 是否为回文子串, 0 代表区间不是回文子串,1 代表区间是回文子串2、确定初始状态首先将区间长度为1的都设为1,即dp[i][原创 2021-03-18 12:20:27 · 300 阅读 · 0 评论 -
动态规划的基本模型
文章目录1258 【例9.2】数字金字塔1259:【例9.3】求最长不下降序列1258 【例9.2】数字金字塔#include <iostream>#include <algorithm>using namespace std;const int N = 1005;int a[N][N], dp[N][N];int main(void){ int r; cin >> r; for (int i = 1; i <= r; i++) for原创 2021-03-13 22:18:23 · 735 阅读 · 4 评论 -
一些题目总结
1.括号配对问题2.最长括号匹配长度杨daolao的博客:https://zhumenger.top/2019/07/27/数据结构基础/括号配对问题/3.Longest Regular Bracket SequenceCodeForces - 5C题解:https://blog.csdn.net/weixin_43772166/article/details/100050789#C__...原创 2019-09-02 21:23:10 · 264 阅读 · 0 评论 -
乌龟棋(动态规划)
小明过生日的时候,爸爸送给他一副乌龟棋当作礼物。乌龟棋的棋盘只有一行,该行有 N 个格子,每个格子上一个分数(非负整数)。棋盘第 1 格是唯一的起点,第 N 格是终点,游戏要求玩家控制一个乌龟棋子从起点出发走到终点。乌龟棋中共有 M 张爬行卡片,分成 4 种不同的类型(M 张卡片中不一定包含所有 4 种类型的卡片),每种类型的卡片上分别标有1、2、3、4 四个数字之一,表示使用这种卡片后,乌龟棋子将向前爬行相应的格子数。游戏中,玩家每次需要从所有的爬行卡片中选择一张之前没有使用过的爬行卡片,控制乌龟原创 2021-02-09 21:07:08 · 576 阅读 · 3 评论 -
poj2279 Mr. Young‘s Picture Permutations
DescriptionMr. Young wishes to take a picture of his class. The students will stand in rows with each row no longer than the row behind it and the left ends of the rows aligned. For instance, 12 students could be arranged in rows (from back to front) of 5原创 2021-01-16 21:27:49 · 207 阅读 · 0 评论 -
poj1191 棋盘分割(记忆化搜索)
题目传送门:http://poj.org/problem?id=1191Description将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的部分继续如此分割,这样割了(n-1)次后,连同最后剩下的矩形棋盘共有n块矩形棋盘。(每次切割都只能沿着棋盘格子的边进行)原棋盘上每一格有一个分值,一块矩形棋盘的总分为其所含各格分值之和。现在需要把棋盘按上述规则分割成n块矩形棋盘,并使各矩形棋盘总分的均方差最小。均方差 σ=Σi=1n(xi−xˉ)2nσ=\sqrt \原创 2021-01-16 13:53:47 · 323 阅读 · 1 评论 -
数字三角形(动态规划)
确定状态为分数的累加和,边界位置只能由上面转移而来,其他位置则可以由正上方和左上方转移而来#include <bits/stdc++.h>using namespace std;const int N = 505;int arr[N][N], dp[N][N];int main(void){ int n; cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j &..原创 2021-01-10 10:10:34 · 623 阅读 · 0 评论 -
字符串编辑(动态规划)
给定两个字符串 str1str1str1 和 str2str2str2,再给定三个整数 icicic、dcdcdc、和 rcrcrc 分别代表插入、删除和替换一个字符的代价。返回将 str1str1str1 编辑成 str2str2str2 的最小代价。比如,str1=“abc”str1=“abc”str1=“abc”,str2=“adc”str2=“adc”str2=“adc”,ic=5ic=5ic=5,dc=3dc=3dc=3,rc=2rc=2rc=2。把 bbb 替换成 ddd 是代价最小的,所以返原创 2020-06-08 21:25:26 · 757 阅读 · 0 评论 -
暴力枚举、搜索、动态规划
四种面值的货币:1, 5, 10, 25,每种均有无数张可用,给定一个整数aim表示要凑齐的金额,计算有多少种方法可以凑齐?可以看出,上面的题目是一个完全背包问题,我们尝试用五种方法来对它进行求解,来体会不同方法的差异,了解动态规划的推导暴力枚举方法#include <iostream>using namespace std;int v[] = {1, 5, 10, 25};int main(void){ int res = 0, aim;原创 2020-06-07 19:29:41 · 2386 阅读 · 0 评论 -
H - Tickets
Jesus, what a great movie! Thousands of people are rushing to the cinema. However, this is really a tuff time for Joe who sells the film tickets. He is wandering when could he go back home as early as...原创 2019-08-27 13:52:44 · 194 阅读 · 0 评论 -
A - Max Sum Plus Plus
Now I think you have got an AC in Ignatius.L’s “Max Sum” problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are faced with a more difficult problem.Given ...原创 2019-08-24 19:38:33 · 117 阅读 · 0 评论 -
背包问题空间复杂度的一维优化
01背包的一维优化二维做法二维递推式:dp[i+1][j]=max(dp[i][j],dp[i][j−w[i]]+v[i])dp[i+1][j]=max(dp[i][j],dp[i][j-w[i]]+v[i])dp[i+1][j]=max(dp[i][j],dp[i][j−w[i]]+v[i])核心代码:for(int i=0;i<n;i++) for(int j=0;j<...原创 2019-08-13 23:57:49 · 1041 阅读 · 0 评论 -
F - Piggy-Bank
DescriptionBefore ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behi...原创 2019-07-28 21:28:24 · 179 阅读 · 0 评论 -
L - Common Subsequence
Sample Inputabcfbc abfcabprogramming contestabcd mnpSample Output420基础dp中很常见的最长公共子序列问题(LCS)#include<stdio.h>#include<string.h>#include<algorithm>u...原创 2019-07-28 17:16:05 · 211 阅读 · 0 评论 -
E - Super Jumping! Jumping! Jumping! N - Longest Ordered Subsequence
OutputFor each case, print the maximum according to rules, and one line one case.Sample Input3 1 3 24 1 2 3 44 3 3 2 10Sample Output4103这道题让找如何走能够得到最多的分数,只能从左往右走,从小数到大数。看着n的范围只有1000,就想着暴...原创 2019-07-28 16:24:43 · 160 阅读 · 0 评论 -
G - 免费馅饼
Sample Input65 14 16 17 27 28 30Sample Output4这是一道比较基础的dp练手题,规定arr[i][j]为第i秒j位置掉落的馅饼,dp[i][j]为第i秒时在j位置最多能够得到的馅饼数。如果正向推导的话,每个状态应为此状态掉落的馅饼加上能够到达此状态的三个或两个状态中馅饼数最多的那个状态数。此状态转移方程如下:dp[i][j]...原创 2019-07-27 23:41:39 · 118 阅读 · 0 评论 -
I - 最少拦截系统
某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹.怎么办呢?多搞几套系统呗!你说说倒蛮容易,成本呢?成本是个大问题啊.所以俺就到这里来求救了,请帮助计算一下最少需要多少套拦截系统....原创 2019-07-24 20:02:37 · 433 阅读 · 0 评论 -
动态规划常见模型
动态规划(Dynamic Programming,简称 DP),是一种通过把复杂问题分解为相对简单的子问题来求解的方法。对于动态规划问题的求解,大致分为三步:1.确定状态2.确定初始状态3.确定状态转移方程基础DP主要有以下五个常见模型:1.递推、递归(以斐波那契数列为例)2.背包问题(01背包、完全背包、多重背包)3.LCS(最长公共子序列问题)4.LIS(最长上升子序列问题)5.最大子段和(最大连续子序列和)原创 2019-07-04 13:28:23 · 3575 阅读 · 0 评论 -
DP经典题
分苹果n 只奶牛坐在一排,每个奶牛拥有 ai 个苹果,现在你要在它们之间转移苹果,使得最后所有奶牛拥有的苹果数都相同,每一次,你只能从一只奶牛身上拿走恰好两个苹果到另一个奶牛上,问最少需要移动多少次可以平分苹果,如果方案不存在输出 -1。拼凑面额给你六种面额1、5、10、20、50、100元的纸币,假设每种币值的数量都足够多,编写程序求组成N元(N为0-10000的非负整数)的不同组合的个数...原创 2019-05-05 19:30:28 · 3640 阅读 · 0 评论 -
B - Ignatius and the Princess IV
“OK, you are not too bad, em… But you can never pass the next test.” feng5166 says.“I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell ...原创 2019-05-04 16:00:26 · 97 阅读 · 0 评论 -
区间DP模板
排成一排,复杂度为O(n3)O(n^3)O(n3)#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int N = 105;const int INF = 0x3f3f3f3f;int...原创 2019-12-07 11:33:22 · 189 阅读 · 0 评论 -
B - Halloween Costumes(区间dp)
InputInput starts with an integer T (≤ 200), denoting the number of test cases.Each case starts with a line containing an integer N (1 ≤ N ≤ 100) denoting the number of parties. Next line contains ...原创 2019-08-12 20:47:31 · 480 阅读 · 0 评论 -
洛谷 P1880 [NOI1995]石子合并(区间dp)
题目描述在一个圆形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分。试设计出1个算法,计算出将N堆石子合并成1堆的最小得分和最大得分.输入格式数据的第1行试正整数N,1≤N≤100,表示有N堆石子.第2行有N个数,分别表示每堆石子的个数.输出格式输出共2行,第1行为最小得分,第2行为最大得分.输入输...原创 2019-08-04 20:24:28 · 212 阅读 · 1 评论 -
E - Multiplication Puzzle(区间dp)
InputThe first line of the input contains the number of cards N (3 <= N <= 100). The second line contains N integers in the range from 1 to 100, separated by spaces.OutputOutput must contain...原创 2019-08-03 22:19:55 · 179 阅读 · 0 评论 -
C - Brackets(区间dp)
DescriptionInputThe input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters (, ), [, and ]; each input test will have length ...原创 2019-08-03 15:48:35 · 650 阅读 · 0 评论 -
A - Cheapest Palindrome(区间dp)
Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow an electronic ID tag that the system will read as the cows pass by...原创 2019-12-05 14:56:14 · 207 阅读 · 0 评论 -
B - Monkey Party(区间dp)
Far away from our world, there is a banana forest. And many lovely monkeys live there. One day, SDH(Song Da Hou), who is the king of banana forest, decides to hold a big party to celebrate Crazy Banan...原创 2019-12-07 11:04:58 · 254 阅读 · 0 评论 -
C - You Are the One(区间dp)
The TV shows such as You Are the One has been very popular. In order to meet the need of boys who are still single, TJUT hold the show itself. The show is hold in the Small hall, so it attract a lot o...原创 2019-12-08 15:36:35 · 167 阅读 · 0 评论 -
D - Palindrome subsequence(区间dp)
In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example, the sequence <A, B...原创 2019-12-12 14:15:52 · 137 阅读 · 0 评论 -
最长回文子串(区间dp)
输入一个字符串Str,输出Str里最长回文子串的长度。回文串:指aba、abba、cccbccc、aaaa这种左右对称的字符串。串的子串:一个串的子串指此(字符)串中连续的一部分字符构成的子(字符)串例如 abc 这个串的子串:空串、a、b、c、ab、bc、abcInput输入Str(Str的长度 <= 1000)Output输出最长回文子串的长度L。Sample InputdaabaacSample Output5区间dp如果dp[i][j]代表区间中最长回文子串长度的话,原创 2020-06-15 20:24:38 · 634 阅读 · 0 评论 -
最长回文子序列(区间dp)
输入一个字符串str,输出str里最长回文子序列的长度。默认情况下,子序列可以不连续初始状态,每个字符自己作为一个回文子序列,即长度为1的区间回文子序列长度为1初始状态,每个字符自己作为一个回文子序列,即长度为1的区间回文子序列长度为1初始状态,每个字符自己作为一个回文子序列,即长度为1的区间回文子序列长度为1转移方程:转移方程:转移方程:dp[i][j]={dp[i+1][j−1]+2s[i]==s[j]max(dp[i+1][j],dp[i][j−1])s[i]!=s[j]dp[i][j]=\原创 2020-06-16 10:04:23 · 259 阅读 · 0 评论 -
A - 不要62(数位dp)
杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer)。杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。不吉利的数字为所有含有4或62的号码。例如:62315 73418 88914都属于不吉利号码。但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列。你...原创 2019-12-12 16:39:07 · 160 阅读 · 0 评论 -
B - Bomb(数位dp)
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...原创 2019-12-15 09:40:13 · 231 阅读 · 0 评论 -
数位dp总结 之 从入门到模板(转)
看到的一位大神的文章,写的太好了基础篇数位dp是一种计数用的dp,一般就是要统计一个区间[le,ri]内满足一些条件数的个数。所谓数位dp,字面意思就是在数位上进行dp咯。数位还算是比较好听的名字,数位的含义:一个数有个位、十位、百位、千位…数的每一位就是数位啦!之所以要引入数位的概念完全就是为了dp。数位dp的实质就是换一种暴力枚举的方式,使得新的枚举方式满足dp的性质,然后记忆化就可以了...转载 2019-12-15 09:57:01 · 150 阅读 · 0 评论 -
C - B-number(数位dp)
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...原创 2019-12-15 14:49:00 · 378 阅读 · 0 评论 -
A - 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...原创 2019-12-19 14:53:41 · 212 阅读 · 0 评论 -
最长公共上升子序列(LCIS)
文章目录最长公共上升子序列输入格式输出格式数据范围输入样例:输出样例:题解LCSLISLCIS优化代码最长公共上升子序列链接:https://www.acwing.com/problem/content/description/274/熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目。小沐沐先让奶牛研究了最长上升子序列,再让他们研究了最长公共子序列,现在又让他们研究最长公共上升子序列了。小沐...转载 2019-09-29 21:40:55 · 690 阅读 · 0 评论 -
背包题目
278. 数字组合这道题数据较小,二维也能求解。#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int w[105];int dp[105][10005];int main(void){ int n,W; scanf("%d%d",...原创 2019-08-14 02:12:43 · 247 阅读 · 0 评论