随笔分类 - ACM--DP--线性dp
摘要:题目链接:here 题解:对于不带限制的最大字段和我们可以:求一遍前缀和,求出最大值最小值,最后结果 res = max( MAX, SUM - MIN ); 那么对于这道题相当于带了限制:限制最大子段的长度是len:我们可以维护一个前缀和 ,然后结果就是 max( sum[i] - min(sum
阅读全文
摘要:测试地址:here AC_Code: 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 typedef unsigned long long ull; 5 const int maxn = 1010
阅读全文
摘要:Maximum Sum on Even Positions 解题思路: 首先,我们知道:如果选择某个子段倒序,那么子段长度一定是偶数,因为如果是奇数长度,那么原来在奇数位置的数还在奇数位置,原来在偶数位置的数还在偶数位置,这对题目要求来说是没有意义的。 那么有两种情况:第一种: 1,2,3,4,5,
阅读全文
摘要:选点 分析:对于一颗树选出得点的权值的关系为:根节点 < 右子树 < 左子树。所以我们可以按照根节点、右子树、左子树进行 dfs 遍历,然后按照这个 dfs 序求 LIS 即为答案。用二分优化求LIS AC_Code 1 #include <iostream> 2 #include <bits/st
阅读全文
摘要:相似基因 很好的题解 1.dp 基本思路 dp 题基本这么几个步骤: 定义状态。 写出状态转移式。 根据状态转移式找出递推顺序。 处理递推的边界。 找出结果。 我讲解时不会就题论题,而是讲大部分黄绿难度的 dp 题的方法。 当然,dp 题十分灵活,不会看完这篇题解就会做,关键在于大量的练习。 2.状
阅读全文
摘要:数的划分 1 //设f(n,k)为整数n拆分成k个数字的方案数,则可以分以下两种情况讨论。 2 //(1)拆分的结果不包含1的情况:如果不包含1,我们把n拆分成k块时可以看做先将每一块加上个1,则n还剩余n-k,即f(n-k,k) 3 //(2)拆分的结果包含1的情况:那么就直接选一个1,即f(n-
阅读全文
摘要:小a的子序列 【题目描述】小a有一个长度为n的序列,但是他忘了这个序列的样子,他只记得序列中的数大小在[1,V]内,你可以任意选择一些位置,并给它们赋值来组成一段子序列,需要满足序列中的数严格递增,一段子序列的“萌值”定义为序列中除最大数外所有数的乘积,若只有1个数则为1。 他想请你求出所有合法子序
阅读全文
摘要:最长公共子上升序列 AC_Code 1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #include <cstring> 5 #include <string> 6 #include <cmath> 7 #include
阅读全文
摘要:回文字符串 思路:由于要找最少添加的字符使得原字符串变为回文串,那么先将给出的字符串反转,将两字符串做 LCS,得到的是最大的公共子串的长度,那么用字符串长度减去最大公共子串长度就是最少添加字符的个数 1 #include <iostream> 2 #include <cstdio> 3 #incl
阅读全文
摘要:友好城市 解题思路:不交叉,则将北岸的坐标从小到大排,找南岸的最长上升子序列 AC_Code 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <algorithm> 5 #include <bits/st
阅读全文
摘要:免费馅饼 数字三角形变形: AC_Code 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <cmath> 6 #include <algorithm> 7 u
阅读全文
摘要:【最长子序列和】 问题定义:对于给定序列 a1,a2,a3……an 寻找它的某个连续子段,使得其和最大。 模板: 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn =
阅读全文
摘要:https://www.cnblogs.com/wsy107316/p/11502628.html 导弹拦截 解题思路:用nlogn的方法求第一问:最长不上升序列:方法链接 用贪心法求第二问,遍历一遍 AC_Code: 1 #include <iostream> 2 #include <cstdio
阅读全文
摘要:登山(LIS变形) 注意读题:不连续两个相同海拔,所以要么严格递增,要么严格递减 AC_Code 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <cmath>
阅读全文
摘要:挖地雷(LIS变形) AC_Code: 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <cstdlib> 6 #include <string> 7 #incl
阅读全文
摘要:Monkey and Banana AC_Code: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 typedef long
阅读全文