动态规划dp
动态规划题解
konghhhhh
记录自己的学习过程
展开
-
数组中01,可以将数组的一段翻转(0 变 1, 1 变 0),求翻转后数组最多1的个数
这道题的关键思想:翻转 0 变 1 ,相当于让 1 的个数 +1翻转 1 变 0 ,相当与让 1 的个数 -1解题思路:根据原来的数组 a[] 构造一个新的数组 b[],若 a[i] = 0 则 b[i] = 1若 a[i] = 1 则 b[i] = -1对 b[] 求最长子段和,包含最长子段和的区间就是进行01翻转的区间。代码略...原创 2022-04-18 19:14:33 · 1276 阅读 · 0 评论 -
1003 最大连续子序列
最大连续子序列Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)Total Submission(s) : 56 Accepted Submission(s) : 25Problem Description给定K个整数的序列{ N1, N2, ...,原创 2017-07-21 21:46:35 · 177 阅读 · 0 评论 -
1001 Robberies
RobberiesTime Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)Total Submission(s) : 14 Accepted Submission(s) : 1Problem DescriptionThe aspiring Roy the Ro原创 2017-07-16 12:27:03 · 141 阅读 · 0 评论 -
1004 Max Sum
Max SumTime Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)Total Submission(s) : 120 Accepted Submission(s) : 24Problem DescriptionGiven a sequence a[1],a原创 2017-07-23 12:08:40 · 173 阅读 · 0 评论 -
动态规划 03 (采药)
03 采药t代表采药总时间,m代表药的数目,接下来m行,输入采每个药的时间和价值,求最大价值。01背包问题,用一个结构体good,里面c代表时间,w代表价值。用数组f【i】【v】表示前 i件药材花费时间 v,那么可得公式 f【i】【v】=max{f【i-1】【v】,f【i-1】【v-c【i】】},即第 i件药材放或者不放的问题。在实际代码中,二维数组可进行空间优化,用f【j】表示即可,原创 2017-04-17 20:20:47 · 282 阅读 · 0 评论 -
动态规划 01 (最长上升子序列)
01: 最长上升子序列最典型的动态规划问题,#includeusing namespace std;int main(){ int n,i,j,sum=0; int p[1000]; int max[1000]; cin>>n; for(i=0;in;i++) { cin>>p[i]; } int maxx=1; max[0]=1; for(i=1;i原创 2017-04-15 22:34:29 · 366 阅读 · 0 评论 -
动态规划 02 (最大子矩阵)
02 最大子矩阵求一个n*n方阵中最大的子矩阵。这是一个二位最大子矩阵问题,做法是将二位压缩至一维,再进行求解。压缩过程中,从第一行往下,先依次压缩入b【】,并比较求出最大子串,直至到最后一行结束。要注意的是b【k】每次 i变化时要赋值为0.代码如下:#includeusing namespace std;int main(){ int n,i,j,k,ma原创 2017-04-17 20:06:42 · 221 阅读 · 0 评论 -
数塔问题 动态规划
The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard bowling-pin-like triangle like this: 7原创 2017-08-10 22:19:02 · 231 阅读 · 0 评论 -
动态规划 16 (踩方格)
16 踩方格一个方格矩阵,矩阵边界在无穷远处。我们做如下假设:a. 每走一步时,只能从当前方格移动一格,走到某个相邻的方格上;b. 走过的格子立即塌陷无法再走第二次;c. 只能向北、东、西三个方向走;请问:如果允许在方格矩阵上走n步,共有多少种不同的方案。2种走法只要有一步不一样,即被认为是不同的方案。这个问题易得出递推公式 f[i]=f[i-1]原创 2017-04-17 22:14:27 · 956 阅读 · 0 评论 -
动态规划 04 (公共子序列)
04 公共子序列给两个字符串,求两个字符串的最长的公共子字符串。用dp【i】【j】表示s的第 i个字符与ss的第 j个字符的子序列长度,要注意s,ss是从【0】开始的,所以dp【i】【j】实际上是s【i-1】与ss【j-1】的结果。公式:当s【i-1】==ss【j-1】时, dp【i】【j】=dp【i-1】【j-1】+1;不等于时,dp【i】【j】=max{dp【i】【j-1】,d原创 2017-04-17 20:33:37 · 217 阅读 · 0 评论 -
动态规划 05 (吃糖果)
05 吃糖果这个问题就是斐波那契数列问题。代码如下:#includeusing namespace std;int main(){ int n,i; cin>>n; int *p=new int[n]; p[1]=1; p[2]=2; for(i=3;in;i++) p[i]=p[i-1]+p[i-2]; coutp[n]; delete []p; r原创 2017-04-17 21:57:53 · 327 阅读 · 0 评论 -
LIS 最长上升子序列 (n*logn) 模板 (二分查找+递归)
最长上升子序列是很早就接触了的问题了,一直用的是动态规划n*n的方法,也知道那不是最好的,可以优化,今天看博客无意中看到LIS,LCS两个词,就特意找了博客看了看,主要是理解一下这里的思想,其实蛮复杂难懂的,自己很难说清楚,还是得引用人家的博客才行。点击打开链接下面还有模板:#include#includeusing namespace std;int a[1000];in原创 2018-01-03 22:06:33 · 731 阅读 · 0 评论 -
动态规划 06 (登山)
06 登山PKU-ACM队组织大家去登山观光,队员们发现山上一个有N个景点,并且决定按照顺序来浏览这些景点,即每次所浏览景点的编号都要大于前一个浏览景点的编号。并且队员不连续浏览海拔相同的两个景点,并且一旦开始下山,就不再向上走了。求最多能浏览的景点。刚开始我以为这是最长上升子序列问题,交上去不对,仔细看看才发现队员还可以下山,即求一个上升加下降子序列,所以我从前往后求一遍最长上升子序原创 2017-04-17 22:01:59 · 807 阅读 · 0 评论 -
UESTC - 594 我要长高(DP+滚动数组优化+单调队列优化)
韩父有NN个儿子,分别是韩一,韩二…韩NN。由于韩家演技功底深厚,加上他们间的密切配合,演出获得了巨大成功,票房甚至高达20002000万。舟子是名很有威望的公知,可是他表面上两袖清风实则内心阴暗,看到韩家红红火火,嫉妒心遂起,便发微薄调侃韩二们站成一列时身高参差不齐。由于舟子的影响力,随口一句便会造成韩家的巨大损失,具体亏损是这样计算的,韩一,韩二…韩NN站成一排,损失即为C×C×(韩ii与韩i原创 2018-01-07 21:37:16 · 407 阅读 · 0 评论 -
2018年全国多校算法寒假训练营练习比赛(第二场)B(TaoTao要吃鸡 ) (01背包特殊处理)
链接:https://www.nowcoder.com/acm/contest/74/B来源:牛客网时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K,其他语言65536K64bit IO Format: %lld题目描述Taotao的电脑带不动绝地求生,所以taotao只能去玩pc版的荒野行动了,和绝地求生一样,游原创 2018-01-29 17:44:47 · 266 阅读 · 0 评论 -
20171119
这几天做的树状dp,虽然还是一个模子的题,但是做起来感觉比之前难了些。很好笑的一个事情,刚开始的时候我还纠结一个题我怎么知道这是个树状dp呢,有了节点,还有了边和权值,那为啥不是个图状dp呢非得是树状dp;结果昨天我正建着树呢才想明白,树是刚好n-1条边啊,少了连不上,多了才是图。另外一个是树的使用了,之前的线段树,树状数组,也都是树,不过又一点都不一样,这次原来苦苦研究明白邻接链表终于有了用处了原创 2017-11-19 22:48:57 · 113 阅读 · 0 评论 -
Q - Godfather
Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all these crimes, and decided to arrest the mafia leaders.Unfortunately, the structure原创 2017-11-26 16:28:23 · 172 阅读 · 0 评论 -
G - Starship Troopers
You, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built underground. It is actually a huge cavern, which consists of many rooms connected with tunnels. Each roo原创 2017-11-26 16:28:03 · 190 阅读 · 0 评论 -
F - TELE
A TV-network plans to broadcast an important football match. Their network of transmitters and users can be represented as a tree. The root of the tree is a transmitter that emits the football match,原创 2017-11-26 16:27:21 · 202 阅读 · 0 评论 -
T - Contestants Division
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-26 16:44:23 · 216 阅读 · 0 评论 -
M - GeoDefense
Tower defense is a kind of real-time strategy computer games. The goal of tower defense games is to try to stop enemies from reaching your bases by building towers which shoot at them as they pass.原创 2017-11-26 22:30:04 · 161 阅读 · 0 评论 -
I - The more, The Better
ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物。但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先攻克其他某一个特定的城堡。你能帮ACboy算出要获得尽量多的宝物应该攻克哪M个城堡吗? Input每个测试实例首先包括2个整数,N,M.(1 = 0。当N = 0, M = 0输入结束。原创 2017-11-26 17:44:33 · 153 阅读 · 0 评论 -
B - Computer
A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Ma原创 2017-11-16 20:44:53 · 188 阅读 · 0 评论 -
R - Tree Cutting
After Farmer John realized that Bessie had installed a "tree-shaped" network among his N (1 Bessie, feeling vindictive, decided to sabotage Farmer John's network by cutting power to one of the bar原创 2017-11-26 16:27:43 · 139 阅读 · 0 评论 -
S - Balancing Act
Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more trees. Define the balance of a node to be the size of the原创 2017-11-19 10:18:44 · 181 阅读 · 0 评论 -
A - Anniversary party
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-16 16:47:23 · 329 阅读 · 0 评论 -
Y - odd-even number
Y - odd-even number 要求一个数的每一位,每连续的奇数要是偶数个,连续的偶数要是奇数个。代码如下:#include#includeusing namespace std;typedef long long LL;int bit[20];LL dp[20][10][20];// pos pre sta LL dfs(in原创 2017-11-09 20:50:10 · 168 阅读 · 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-11-03 21:26:27 · 191 阅读 · 0 评论 -
X-Bi-peak Number
One integer number x is called "Mountain Number" if:(1) x>0 and x is an integer;(2) Assume x=a[0]a[1]...a[len-2]a[len-1](0≤a[i]≤9, a[0] is positive). Any a[2i+1] is larger or equal to a[2i] and a[原创 2017-11-06 22:46:09 · 211 阅读 · 0 评论 -
光大科技 笔试题 1-n的所有数按字典序输出序列
时隔好久做点算法题是真的手生,今天一波笔试效果较差,补道题吧。 题意: 将1到n所有数按字典序排序,输出序列。 思路: 将数字按字典序排列,显然,应该一位一位的分析,开始的思路是数位DP(实际上也确实是),不过错了,这里犯的一个问题就是:我的想法是先求出来n的位数m,然后从最高位向低位走过去,中间从0-9循环,就能出来0001,0002,0003,,,,,0010,0011,,...原创 2019-12-30 23:22:45 · 1321 阅读 · 0 评论 -
U - Investigation
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-07 22:03:46 · 183 阅读 · 0 评论 -
E-Beautiful Numbers (对每一位求模)
Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with原创 2017-11-06 21:09:06 · 300 阅读 · 0 评论 -
R—self同类分步
给出a,b,求出[a,b]中各位数字之和能整除原数的数的个数。Sample Input10 19Sample Output3Hint【约束条件】1 ≤ a ≤ b ≤ 10^18 题意太好理解了,一句话而已。写dfs部分的时候,感觉不要dp了吧,直接深搜暴力深搜就好,结果超时。还是得数位上下手,代码里的求模的那个sum,又学了一手,直接在so原创 2017-11-06 22:40:52 · 189 阅读 · 0 评论 -
C - Apocalypse Someday
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-11 17:01:34 · 203 阅读 · 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-11-03 20:29:59 · 210 阅读 · 0 评论 -
V - How Many Zeroes
Jimmy writes down the decimal representations of all natural numbers between and including m and n, (m ≤ n). How many zeroes will he write down?InputInput starts with an integer T (≤ 11000), denot原创 2017-11-07 23:37:31 · 206 阅读 · 0 评论 -
D - Hie with the Pie
The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can afford to hire only one driver to do the deliveries. He will wait原创 2017-12-04 17:22:25 · 309 阅读 · 0 评论 -
压缩dp的位运算
压缩dp用到了二进制位运算上的东西,整理一下基本内容。 位运算的几个基本操作: 1、移位(>) 2、按位与(&) 3、按位或(|) 4、非(~) 5、异或(&) 每个都很简单,c++上都学过,难的实在那几个巧妙的利用上。 1、判断一原创 2017-12-04 16:55:32 · 246 阅读 · 0 评论 -
M - 方格取数(1)
给你一个n*n的格子的棋盘,每个格子里面有一个非负数。 从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取的数所在的2个格子不能相邻,并且取出的数的和最大。Input包括多个测试实例,每个测试实例包括一个整数n 和n*n个非负数(nOutput对于每个测试实例,输出可能取得的最大的和Sample Input375 15 21 75 15 28 34 7原创 2017-12-08 08:59:47 · 392 阅读 · 0 评论 -
G - Survival
The King of Fighter 97 (KOF97) is an electronic game of wrestling type. Once it was fashionable among youths. The game is amused. However, playing by oneself is not as excited as with friends. Getting原创 2017-12-08 10:37:03 · 136 阅读 · 0 评论