![](https://img-blog.csdnimg.cn/20201014180756738.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
动态规划
Prime me
Nobody can defeat me unless I don't want to win.
展开
-
完全背包问题
完全背包问题与01背包问题主要区别在于,01背包只是在一个物品的选与不选之间做决定,即数量为0或1,而完全背包则是0,1,2,3等。 完全背包状态方程为 f[i][j]=max(f[i-1][j],f[i][j-kv[i]]+kw[i]) #include<iostream> #include<cstring> #include<algorithm> using namespace std; int f[1010][1010]; int v[1010],w[101.原创 2021-11-26 21:36:29 · 178 阅读 · 0 评论 -
背包问题(01背包)
01背包: 动态方程 j<w(i) V(i,j)=V(i-1,j) j>=w(i) V(i,j)=max{V(i-1,j),V(i-1,j-w(i))+v(i)} 模板题:在值与体积不同的石头中选择背包能承载的最大值问题,每个石头只能用一次 01背包模板 #include<iostream> #include<algorithm> #include<cstring> using namespace std; int v[1001],w[1001],dp[1原创 2021-11-09 20:04:14 · 76 阅读 · 0 评论 -
Nun Heh Heh Aaaaaaaaaaa(CCPC网络赛补题)(线性dp+后缀和+思维)
Problem Description Vasily Tadokorov is a stringologist. He thinks a string is fragrant if it can be divided into two parts — nunhehheh as the prefix and a number of (excluding 0) a as the suffix. For example, nunhehhehaaaaaa is fragrant, but nunhehheh an.原创 2021-11-05 20:58:31 · 142 阅读 · 0 评论 -
I I love you(求子序列个数)
链接:https://ac.nowcoder.com/acm/contest/3947/I 来源:牛客网 输入描述: 共一行:一封若干个字符的情书(大小写不敏感)。 情书不会超过684594个字符(大写、小写字母)。 输出描述: 共一行:包含一个整数,即iloveyou在情书中作为子序列出现的次数。 由于答案可能很大,请输出对20010905取模后的值。 IloveyouNotonlyforwhatyouareButforwhatIamWhenIamwithyouIloveyouNotonlyforwh.原创 2021-11-05 19:11:24 · 288 阅读 · 0 评论 -
算法实验8:划分问题(DP)
Description 给定一个正整数的集合A={a1,a2,….,an},是否可以将其分割成两个子集合,使两个子集合的数加起来的和相等。例A = { 1, 3, 8, 4, 10} 可以分割:{1, 8, 4} 及 {3, 10} Input 第一行集合元素个数n n <=300 第二行n个整数 Output 如果能划分成两个集合,输出任意一个子集,否则输出“no” 直接递推出所有结果,判断是否可以到达一半元素和,然后输出。 #include <iostream> #include.原创 2021-10-29 10:25:49 · 644 阅读 · 0 评论 -
A - The Sports Festival (CodeForces - 1509C)(DP)(记忆化搜索)
https://codeforces.com/problemset/problem/1509/C 对 a 数组排序,我们考虑最后一步,增加的价值必然是 sn - s1,那这一步之前只有两个状态只可能是两个状态: 区间(1, n-1)或者区间(2, n)。 对于区间(1, n-1),他的价值必然也是 sn-1 - s1 + 区间(1, n-2)或者区间(2, n-1)。 当 l = r 时,区间(l, r) 的代价为 0。直接记忆化搜索即可。 动态方程是:dp[l][r]=s[r]-s[l]+min(df.原创 2021-10-25 20:33:48 · 92 阅读 · 0 评论 -
E - The Triangle POJ - 1163(DP)
http://poj.org/problem?id=1163 初始化dp[i][j]=f[i][j],然后将下一次运算时加上max(f[i-1][j],f[i-1][j-1]) 动态方程为dp[i][j]=f[i][j]+max(f[i-1][j],f[i-1][j-1]). #include<iostream> #include<cstdio> using namespace std; int f[101][101],dp[101][101]; int main() { .原创 2021-10-25 19:57:01 · 61 阅读 · 0 评论 -
C. Woodcutters
https://codeforces.com/problemset/problem/545/C 砍树问题:在树被砍倒时不能压倒旁边的树,若被砍到则可以。求最多可以砍倒多少棵树。 dp问题:判断是否。典型的动态规划题,一棵树到底砍不砍,取决于倒下来的时候是否与其他树重合,为了保证被砍的树尽可能多,应该判断两棵树之间的距离和两棵树的高度关系,距离较大,则可以砍。 做法:优先向左砍,若不行则向右。 #include <iostream> #include <cstdio> using .原创 2021-10-25 17:18:58 · 150 阅读 · 0 评论 -
最长上升子序列(Acwing 895)&&最长公共子序列(Acwing 3510)&&最大上升字段和(HDU - 1087 )
给定一个长度为 N 的数列,求数值严格单调递增的子序列的长度最长是多少。 输入格式 第一行包含整数 N。 第二行包含 N 个整数,表示完整序列。 输出格式 输出一个整数,表示最大长度。 数据范围 1≤N≤1000, −109≤数列中的数≤109 input 7 3 1 2 1 8 5 6 output 4 最长上升子序列模板,复习备用 #include<stdio.h> #include<iostream> #include<vector> using names.原创 2021-09-12 19:35:13 · 69 阅读 · 0 评论 -
Almost Sorted Array(最长上升子序列o(nlogn))
We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, selection sort, bubble sort, etc. But sometimes it is an overkill to use these algorithms for an almost sorted array. We say an array is sorted if its elements.原创 2021-07-31 22:01:54 · 113 阅读 · 0 评论