杭电46道dp题
skajre
这个作者很懒,什么都没留下…
展开
-
HDU 5904 LCIS(最长上升公共子序列)
这题有点特殊,那就是上升序列是严格递增的,那么只需要访问x的前一个点的连续值来更新x的连续值,时间复杂度O(n)即可。//// main.cpp// Richard//// Created by 邵金杰 on 16/9/27.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#incl原创 2016-09-27 14:16:34 · 225 阅读 · 0 评论 -
HDU 1003 Max sum(dp)
#includeusing namespace std;int arr[100001],dp[100001];int main(){ int t,n,i=1; cin>>t; while(t--) { cin>>n; for(int i=0;i<n;i++) cin>>arr[i]; d原创 2016-09-08 17:20:02 · 134 阅读 · 0 评论 -
HDU 1231 最大连续子序列(dp)
状态方程f[i]表示0~i最长子序列长度。状态转移f[i]=max(f[i],f[i-1]+d[i])。//// main.cpp// Richard//// Created by 邵金杰 on 16/9/8.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#i原创 2016-09-08 17:04:21 · 143 阅读 · 0 评论 -
HDU 1864 最大报销额(01背包)
定义状态f[x]表示x的报销额下最大能报销的费用,状态转移f[j]=max(f[j],f[j-G[i]]+G[i])。//// main.cpp// Richard//// Created by 邵金杰 on 16/9/8.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#inclu原创 2016-09-08 16:27:21 · 164 阅读 · 0 评论 -
HDU 2955 Robberies(01背包)
题目给出一个几率,求出小于这个几率的最多钱,这是典型的01背包。后面给出是m万钱,q的几率被抓。可以反推生存几率来求出被抓几率,所以状态转移就是f[j]=max(f[j],f[j-m[i]]*(1-q[i]))。//// main.cpp// Richard//// Created by 邵金杰 on 16/9/8.// Copyright © 2016年 邵金杰. Al原创 2016-09-08 14:31:55 · 197 阅读 · 0 评论 -
HDU 1502 Regular Words(打表预处理dp)
//// main.cpp// Richard//// Created by 邵金杰 on 16/9/22.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;const int maxn=80;string o原创 2016-09-22 20:47:03 · 181 阅读 · 0 评论 -
HDU 1503 Advanced Fruits(LCS)
找出公共子串只需要顺着dp数组往回找就可以了,还要注意可能两个母串前面很长一段距离都是都没有公共串,所以最后要把x和y退到0。//// main.cpp// Richard//// Created by 邵金杰 on 16/9/17.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#i原创 2016-09-17 08:51:02 · 194 阅读 · 0 评论 -
HDU 1501 Zippe(dp记忆化搜索)
一开始题目看错了,以为前两串的长度只和与第三个串的长度不想等,WA了好久,而且一直没忘搜索想,以前概念里认为dp就是递推,一直没怎么用过记忆化,以后要用记忆化尝试一下。//// main.cpp// Richard//// Created by 邵金杰 on 16/9/16.// Copyright © 2016年 邵金杰. All rights reserved.//原创 2016-09-16 23:15:14 · 261 阅读 · 0 评论 -
HDU 1059 Dividing(dp多重背包)
//// main.cpp// Richard//// Created by 邵金杰 on 16/9/16.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;int v[7];int dp[120000+100]原创 2016-09-16 19:28:08 · 187 阅读 · 0 评论 -
HDU 1025 Constructing Roads In JGShining's Kingdom(LIS)
//// main.cpp// Richard//// Created by 邵金杰 on 16/9/16.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;const int maxn=500000+100;i原创 2016-09-16 11:44:21 · 143 阅读 · 0 评论 -
HDU 1081 To The Max(dp最大子矩阵和)
//// main.cpp// Richard//// Created by 邵金杰 on 16/9/15.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;const int maxn=100+10;int s原创 2016-09-16 08:04:56 · 176 阅读 · 0 评论 -
HDU 1078 FatMouse and Chess(dp记忆化搜索)
//// main.cpp// Richard//// Created by 邵金杰 on 16/9/15.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;const int maxn=100+10;typed原创 2016-09-16 08:04:20 · 153 阅读 · 0 评论 -
HDU 1506 Largest Rectangle in a Histogram(dp求最大子矩阵和)
先用动态规划先找出i左边比他高的,再求出i右边比他高的,最后求出最大面积即可。//// main.cpp// Richard//// Created by 邵金杰 on 16/9/8.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing na原创 2016-09-08 19:55:17 · 182 阅读 · 0 评论 -
HDU 1505 City Game(dp)
跟1505差不多,定底定高就可以。//// main.cpp// Richard//// Created by 邵金杰 on 16/9/8.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;const int原创 2016-09-08 21:57:15 · 164 阅读 · 0 评论 -
HDU 1300 Pearls(dp)
这题题目里说了,给出的数据是有一定递增的,所以不用排序。//// main.cpp// Richard//// Created by 邵金杰 on 16/9/25.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;原创 2016-09-25 09:38:35 · 209 阅读 · 0 评论 -
HDU 1423 Greatest Common Increasing Subsequence(LCIS)
//// main.cpp// Richard//// Created by 邵金杰 on 16/9/27.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;const int maxn=500+10;int原创 2016-09-27 16:24:26 · 176 阅读 · 0 评论 -
HDU 4512 完美队列I(LCIS)
这题的关键点就是第二位的限制是n-i+1,这样就会把正在匹配的a[i]也算入其中,目的是为了有可能最后的长度是奇数的,那么有一个点是不对称的,所以是自己匹配自己,只要特殊判断一下即可。//// main.cpp// Richard//// Created by 邵金杰 on 16/9/27.// Copyright © 2016年 邵金杰. All rights reserved原创 2016-09-27 18:15:56 · 414 阅读 · 0 评论 -
HDU 1074 Doing Homework(状压dp)
输出路径时要注意,越后面的作业越后输出,所以正序遍历时,一旦有成绩小于等于马上更新pre指针,那样的话就能保证做作业是字典序了。//// main.cpp// Richard//// Created by 邵金杰 on 16/9/27.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include原创 2016-09-27 10:21:45 · 188 阅读 · 0 评论 -
HDU 1227 Fast Food(dp)
这题状态方程定义不难,难的是状态的转移,如果要在i~j之间设仓库,那么仓库一定是中间那个点,根据这个性质,先初始化1~i的设一个仓库的长度,然后从设2个仓库开始dp,转移方程为dp[i][j]=min(dp[i][j],dp[i-1][m]+cost[m][[j]),m的范围为i-1~j-1,代表1~m设立了1-i个点,然后在m~j设立了一个点,那么刚好可以转移。//// main.cpp原创 2016-09-26 20:50:21 · 222 阅读 · 0 评论 -
HDU 1158 Employment Planning(dp)
//// main.cpp// Richard//// Created by 邵金杰 on 16/9/26.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;const int maxn=15;int h,s,原创 2016-09-26 19:02:46 · 191 阅读 · 0 评论 -
HDU 3311 Hie with the Pie(状压dp+TSP问题)
因为每个点可以重复走,所以从这一个点到下一个点走最短路。//// main.cpp// Richard//// Created by 邵金杰 on 16/9/26.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;原创 2016-09-26 14:37:57 · 233 阅读 · 0 评论 -
HDU 2059 龟兔赛跑(dp)
//// main.cpp// Richard//// Created by 邵金杰 on 16/9/26.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;const int maxn=100+10;doub原创 2016-09-26 10:29:48 · 212 阅读 · 0 评论 -
HDU 3008 Warcraft(dp)
//// main.cpp// Richard//// Created by 邵金杰 on 16/9/25.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;const int maxn=100+10;int原创 2016-09-25 21:25:07 · 308 阅读 · 0 评论 -
HDU 1422 重温世界杯(最长正子段长度)
//// main.cpp// Richard//// Created by 邵金杰 on 16/9/25.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;const int maxn=100000+100;原创 2016-09-25 19:05:07 · 306 阅读 · 0 评论 -
HDU 1080 Human Gene Functions(变形的LCS)
这题一开始写成了先算出LCS长度,然后往回跑,把公共子串剔除,然后算值,最后发现这样做答案就固定了= =,无法求出最大值了。后来看到题解,恍然大悟,发现自己实在太蠢了,这题三种选择,对应字符匹配,上字符对应-,下字符对应-,然后LCS框架上去了就可以了。#include#include#include#include#includeusing namespace std;const原创 2016-09-25 15:06:21 · 182 阅读 · 0 评论 -
HDU Free DIY Tour(dp)
//// main.cpp// Richard//// Created by 邵金杰 on 16/9/25.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;const int maxn=1000+10;int原创 2016-09-25 12:47:19 · 215 阅读 · 0 评论 -
HDU 1160 FatMouse's Speed(LIS)
//// main.cpp// Richard//// Created by 邵金杰 on 16/9/15.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;const int maxn=1000+10;int原创 2016-09-16 08:03:11 · 135 阅读 · 0 评论 -
HDU 1081 To The Max(dp最大子矩阵和)
//// main.cpp// Richard//// Created by 邵金杰 on 16/9/15.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;const int maxn=100+10;int s原创 2016-09-16 00:11:06 · 144 阅读 · 0 评论 -
HDU 1078 FatMouse and Chess(dp记忆化搜索)
//// main.cpp// Richard//// Created by 邵金杰 on 16/9/15.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;const int maxn=100+10;typed原创 2016-09-15 21:17:28 · 151 阅读 · 0 评论 -
HDU 2159 FATE(二维完全背包)
二位背包其实和一维一样,只是多了一个限制条件,一维一般是体积,这里二维是耐力和杀敌数,因为求最小耐力,所以耐力循环在杀敌循环外面,并且想要拿到经验,最少都要从b[i]开始。完全背包是循环是顺序的。//// main.cpp// Richard//// Created by 邵金杰 on 16/9/13.// Copyright © 2016年 邵金杰. All rights re原创 2016-09-13 16:37:00 · 210 阅读 · 0 评论 -
HDU 1203 I NEED A OFFER!(01背包)
//// main.cpp// Richard//// Created by 邵金杰 on 16/9/13.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;const int maxn=10000+100;do原创 2016-09-13 15:42:28 · 145 阅读 · 0 评论 -
HDU 1176 免费馅饼(dp)
这题就是数塔,但是我一开始没仔细看题,以为从第随便走走到顶最大,实际上是从5这个点出发,那么倒过来dp数塔就可以了。//// main.cpp// Richard//// Created by 邵金杰 on 16/9/13.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#includ原创 2016-09-13 14:58:03 · 195 阅读 · 0 评论 -
HDU 2084 数塔
状态方程f[j]=max(f[j]+tri[i][j],f[j+1]+tri[i][j])。//// main.cpp// Richard//// Created by 邵金杰 on 16/9/9.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeu原创 2016-09-10 10:40:36 · 230 阅读 · 0 评论 -
HDU 1171 Big Event in HDU(多重背包)
f[j]=max(f[j],f[j-k*v[i]]+k*v[i])。内层循环时逆序。//// main.cpp// Richard//// Created by 邵金杰 on 16/9/10.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing原创 2016-09-10 10:22:13 · 169 阅读 · 0 评论 -
HDU 1114 Piggy-Bank(dp完全背包)
状态转移f[j]=min(f[j],f[j-w[i]]+p[i])。内层循环为递增。原创 2016-09-10 08:34:49 · 180 阅读 · 0 评论 -
HDU 1069 Monkey and Banana(dp最长上升子序列)
方程f[i]=max(f[i],f[j]+h[i])。//// main.cpp// Richard//// Created by 邵金杰 on 16/9/9.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#include#includeusing names原创 2016-09-09 20:26:41 · 176 阅读 · 0 评论 -
HDU 2571 命运(dp)
//// main.cpp// Richard//// Created by 邵金杰 on 16/9/9.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;const int maxn=1000+10;cons原创 2016-09-09 10:49:21 · 206 阅读 · 0 评论 -
HDU 2391 Filthy Rich(dp)
//// main.cpp// Richard//// Created by 邵金杰 on 16/9/9.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;const int maxn=1000+10;int原创 2016-09-09 10:10:45 · 159 阅读 · 0 评论 -
HDU 1087 Super Jumping! Jumping! Jumping!(dp最长上升子序列的和)
f[i]=max(f[i],f[j]+d[i])。//// main.cpp// Richard//// Created by 邵金杰 on 16/9/9.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#includeusing namespace std;c原创 2016-09-09 09:37:36 · 146 阅读 · 0 评论 -
HDU 2577 How to Type(dp)
当在第i个字符时,会有两种情况,一种是灯关着,另一种是灯开着,所以要分别进行dp,看最后哪种少。//// main.cpp// Richard//// Created by 邵金杰 on 16/9/13.// Copyright © 2016年 邵金杰. All rights reserved.//#include#include#include#include原创 2016-09-13 19:18:07 · 129 阅读 · 0 评论