动态规划
动态规划
懒狗的救赎
HUMBLE.
展开
-
最长公共子序列【动态规划】
#include <iostream>#include <cstring>using namespace std;string a,b;int dp[100][100];int fun(int n,int m){ //n,m分别表示两个字符串当前最长公共子序列的位置 if(dp[n][m]>=0) return dp[n][m]; int ans; if(n==0||m==0){ //如果到达起始位置 if(原创 2021-03-20 14:59:52 · 55 阅读 · 0 评论 -
KY75 采药
#include <iostream>#include <algorithm>#include <cstring>#include <map>using namespace std;int use[1005];int value[1005];int dp[105][1005];int fun(int n,int t){ if(dp[n][t]>0) return dp[n][t]; int ans;原创 2021-03-17 17:19:16 · 168 阅读 · 0 评论 -
KY73 合唱队形【最大上升子序列的应用】
原理:最大上升子序列此题思路分析:https://www.bilibili.com/video/BV1qy4y1e7bX?from=search&seid=1474087574309940493代码:#include <iostream>#include <algorithm>#include <cstring>#include <map>using namespace std;int a[105];int dp1[105];i原创 2021-03-17 16:55:56 · 140 阅读 · 0 评论 -
KY78 最大上升子序列和【与某题几乎相同】
分析:地址代码:#include <iostream>#include <algorithm>#include <cstring>#include <map>using namespace std;int a[1005];int dp[1005];int fun(int n){ if(dp[n]>=0) return dp[n]; int ans; if(n==0) ans =原创 2021-03-17 15:33:45 · 367 阅读 · 0 评论 -
KY66 点菜问题【0-1背包问题】
记忆性递归法:此题与之前的神奇的口袋极其相似。#include <iostream>#include <cstring>#include <algorithm>using namespace std;int cost[105];int score[105];int dp[105][1005]; //dp记忆方程int fun(int n,int money){ //n为当前的下标,money为剩余的钱 if(dp[n][money]>原创 2021-03-16 18:05:30 · 212 阅读 · 0 评论 -
KY59 神奇的口袋【和某题相同原理!!】
前身题此题将之前代码的或者符号改成加号暴力递归:#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;int a[20];int fun(int n,int M){ //当前为结尾凑成的最少张数; if(M==0) return 1原创 2021-03-07 15:34:41 · 98 阅读 · 0 评论 -
最长上升子序列【动态规划】
【题目描述】给定N个数,求这N个数的最长上升子序列的长度。【样例输入】53 7 1 2 5【样例输出】3思路:对于每个位置的数值而言,最小的上升序列是其本身,即长度为1。因此在循环当前数值之前的数据时候,只有比他小的才能加入以他为结尾的最长上升序列。不妨记fun(n)为:以第n个数据为结尾的最长上升序列个数。则,以上述的样例得:fun(1)=1【没有前序,则为本身长度】fun(2)=2【 3 7 所组成的长度】fun(3)=1【前序都比1大,则为本身长度】fun(4原创 2021-03-07 14:09:02 · 152 阅读 · 0 评论 -
KY41 放苹果
分析:设f(m,n)为m个苹果,n个盘子的放法数目,则先对n作讨论,当n>m:则必定有n-m个盘子永远空着,去掉它们对摆放苹果方法数目不产生影响。即 if(n>m) f(m,n) = f(m,m)当n<= m:不同的放法可以分成两类:含有0的方案数,不含有0的方案数 含有0的方案数,即有至少一个盘子空着,即相当于f(m,n)=f(m,n-1);不含有0的方案数,即所有的盘子都有苹果,相当于可以从每个盘子中拿掉一个苹果,不影响不同放法的数目,即f(m,n)=f(m-n,n)原创 2021-03-07 12:07:48 · 118 阅读 · 0 评论 -
KY255 N阶楼梯上楼问题【动态规划】
记忆性递归:#include <iostream>using namespace std;int dp[91]={0}; //到每层的方式次数int fun(int n){ //记忆性递归 if(dp[n]>0) return dp[n]; int ans; if(n==1) ans = 1; else if(n==2) ans = 2; //第二个台阶有连续跳两次,和一次直接跳原创 2021-03-07 10:08:04 · 71 阅读 · 0 评论 -
能否挑选出给定和【动态规划】
#include <iostream>using namespace std;int a[10];bool dp[10][20]={0}; //表示到当前点处的最大值bool fun(int n,int s){ //记忆递归 if(dp[n][s]>0){ return dp[n][s]; } bool ans; if(s==0) ans = true; else if(n==0) .原创 2021-03-07 09:44:22 · 59 阅读 · 0 评论 -
挑选最大和【动态规划】
点击题目:在一堆数字中,选择数字之和最大,无法联系选择,必须相隔。代码:#include <iostream>#include <cstring>#include <cmath>#include <algorithm>using namespace std;int a[10];int dp[10]; //表示到当前点处的最大值int fun(int n){ //无记忆递归 if(n==0) r原创 2021-03-07 00:30:41 · 169 阅读 · 0 评论 -
KY8 整数拆分
思路:对于一个数字 n,如果 n 是奇数,那么 n 的所有组合方式中一定包含一个 1,那么它和 n-1 的组合方式种数相同,f[n] = f[n-1];如果 n 是偶数,那么它的组合方式中可能有 1,也可能没有 1,有 1 的组合方式有 f[n - 1] 种,没有 1 的组合方式有 f[n/2] 种,因为偶数组合方式除以 2 后的组合方式其实是一样的,f[n] = f[n-1] + f[n/2]代码:#include <iostream>#include <cstring>原创 2021-03-06 20:46:00 · 286 阅读 · 0 评论 -
KY22 最大序列和
记忆递归法:#include <iostream>#include <climits>#include <cstring>#include <algorithm>const long INF = LONG_MAX;using namespace std;long long a[1000005];long long memo[1000005]; //记录已经计算过的数值long long fun(long n){ if(memo[n]原创 2021-03-06 17:30:49 · 178 阅读 · 0 评论