动态规划
文章平均质量分 72
saucyJack
外科
展开
-
poj 3368 Frequent values(RMQ)
也算是一道模版题吧。 初始化为连续相同的数的个数,最后查询之前先从和区间第一个数不同的数开始查询,然后和区间内和第一个数相同的数的个数进行比较,输出最大值就好了。 #include #include #include #include #include using namespace std; const int INF=0x3f3f3f3f; const int maxn=100005;原创 2016-02-08 23:37:23 · 204 阅读 · 0 评论 -
poj 3264 Balanced Lineup(RMQ && 线段树)
题目链接:http://poj.org/problem?id=3264 题意:求出一个给定的区间的最大差值。 很典型的RMQ问题,也可以用线段树做。 ①RMQ: ②线段树(1735ms): #include #include #include #include using namespace std; const int INF=0x3f3f3f3f; const原创 2016-02-08 21:48:57 · 248 阅读 · 0 评论 -
POJ 1276(多重背包)
题目链接:http://poj.org/problem?id=1276 一道很有意思的题目。 做完这道题后发现了很多神奇的东西,原来多重背包问题还可以转化成完全背包问题。 另外,也更多理解了动态规划,稍微明白了一点动归和搜索的差别,还需要继续努力。 别人的解题报告:http://blog.csdn.net/lyy289065406/article/details/6648102原创 2016-02-02 12:37:38 · 263 阅读 · 0 评论 -
POJ 1837(01背包)
题目链接:http://poj.org/problem?id=1837 这道题目思考了很久都没写出状态转移方程,各种BUG,最后还是去往上搜了一下题解。 #include #include using namespace std; const int maxn=15050; int C,G; int c[35],g[35]; int dp[35][maxn]; //dp[i][j]表示挂满前原创 2016-02-02 10:29:55 · 275 阅读 · 0 评论 -
poj 1018 Communication System(DP)
题意: 一个通信系统需要n个设备,每个设备有两个属性带宽B和价格P,第i个设备有mi个厂家生产,从中挑选n个设备,问n个设备中最小的带宽/总价格(B/P)的最大值是多少; 思路: dp[i][j]表示前i个设备中带宽为j的最小费用(相同带宽费用越小,B/P越大) dp[i][j]=min(dp[i][j],dp[i-1][j]+p); #include #include #include原创 2016-03-23 13:26:41 · 278 阅读 · 0 评论 -
poj 1050 To the Max(最大子矩阵和)
题意:给出一个N^2的矩阵,求最大子矩阵和。 纯暴力应该是N^6,枚举从(i,j)到(p,q)的所有情况,把之间所有点相加计算总和,这样会导致很多数据进行重复的计算,复杂度太高,直接TLE,后来进行优化,先对数据做了一些预处理,记录点(1,1)到(i,j)的子矩阵和,然后枚举N^2个点,粗略估计复杂度为(n^4)/2,5kw,交上去63ms居然过了!继续思考了一下,可以转化为最大连续子序列的和的原创 2016-03-23 18:14:10 · 302 阅读 · 0 评论 -
poj 1088 滑雪(记忆化搜索+dp)
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子 1 2 3 4 5 16 17 18 19 6 15 24 25原创 2016-03-24 09:44:08 · 247 阅读 · 0 评论 -
poj 1125 Stockbroker Grapevine(多源最短路)
题目链接:http://poj.org/problem?id=1125 思路:用Floyd预处理任意两点之间的最短距离,然后枚举每个点到其他所有点的最长距离,求出最小距离,如果最小距离不为INF,总有解,输出即可。 在这里,注释一下Floyd的动态规划思想: dp[k][i][j]表示i和j之间可以通过编号为1...k的节点的最短路径。初值dp[0][i][j]为原图的邻接矩阵。原创 2016-03-24 10:42:00 · 210 阅读 · 0 评论 -
Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake(线段树+dp)
官方题解: 首先,我们计算每个蛋糕的体积: vi=π∗hi∗r2i 现在,考虑序列 v1,v2,v3,…,vn :问题的答案是这个序列中递增子序列的最大和。我们怎么样解决它? 首先去掉小数,我们可以定义一个新的数组 a1,a2,a3,…,an,ai=vi/π=hi∗r2i 们考虑 dpi是以 ai结束的序列和的最大值且 dpi=max(ai,maxj 这个问题的答案就是: π∗m原创 2016-03-02 22:56:52 · 345 阅读 · 0 评论 -
poj 2750 Potted Flower(线段树+动态规划)
题目链接:http://poj.org/problem?id=2750 (思考了很久也没做出来,最终还是默默去看了题解....) 思路: sum:表示区间的和 maxNum:表示区间最大值 minNum:表示区间最小值 lmax:表示从区间左端开始的连续最大子序列 rmax:表示从区间右端开始的连续最大子序列 lmin:表示从区间左端开始的连续最小子序列 rmin:表示从区间原创 2016-02-11 11:03:17 · 331 阅读 · 0 评论 -
poj 1185 炮兵阵地(状压dp)
题目链接:http://poj.org/problem?id=1185 状压dp跑了375ms... 状态转移方程是参考网上的思路... dp[i][j][k]表示第i行状态为j,第i-1行状态为k的最优解 开一个三维数组,而大小为2*1 如果筛选出同一行内的可行状态,其实最多只有60种(可以取m最大输出stateNum即可) 状态转移方程为dp[r%2][i][j]=max(dp原创 2016-02-15 17:47:00 · 208 阅读 · 0 评论 -
POJ 1080(最长公共子序列)
题目链接:http://poj.org/problem?id=1080 一道最长公共子序列变形的题目。 关于那个表格,没想到很好的处理方式,直接打表了。 最长公共子序列的状态转移方程: if(a[i]==b[i]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i-1][j],dp[i][j-1]); 这道题需要做一下变形: s1取字母,原创 2016-02-02 16:00:50 · 342 阅读 · 0 评论 -
POJ 3176(动态规划:数塔问题)
题目链接:http://poj.org/problem?id=3176 数塔问题,比较基础。 #include #include #include using namespace std; const int INF=0x3f3f3f3f; const int maxn=400; int n; int a[maxn][maxn]; int main(){ #ifndef ONLINE_JU原创 2016-02-02 14:18:17 · 197 阅读 · 0 评论 -
POJ 1159 Palindrome
题目链接:http://poj.org/problem?id=1159 这道题本质上是求一个子序列和其逆子序列的最长公共子序列(证明略),但是输出的是未匹配的字符数。 数据比较大,5000*5000的数据,开数组肯定MLE,但是不小心用short睡过去了= =! 正解应该是用滚动数组,把数据缩小到2*5000. 因为当前状态之和前一状态有关,所以进行i%2取余就好了。 #include原创 2016-02-02 17:42:16 · 155 阅读 · 0 评论 -
POJ 2533 3903(最长递增子序列)
题目链接:http://poj.org/problem?id=2533 题目链接:http://poj.org/problem?id=3903 2533是裸的最长递增子序列。 3903需要用到二分优化,否则至改动2533代码的数组大小是过不了的。 POJ 2533: #include #include #include #include using namespace std原创 2016-02-02 18:45:04 · 212 阅读 · 0 评论 -
POJ 1260 Pearls(DP)
题目链接:http://poj.org/problem?id=1260 题意: 按珍珠等级升序给出n种珍珠的个数和价值,每种珍珠可以可以通过购买等级较高的珍珠来代替,问最小价值是多少? 状态转移方程: ①不取代: dp[i]=dp[i-1]+(a[i]+10)*p[i]; ②取代: dp[i]=min(dp[i],dp[j]+(sum[i]-sum[j]+10)*p[i]);原创 2016-02-03 15:55:08 · 206 阅读 · 0 评论 -
POJ 1836 Alignment(LIS变形)
题目链接:http://poj.org/problem?id=1836 题意: 一个队列的士兵,要求选择x个士兵出列,使得剩下的士兵可以看到向左或向右无穷远出,求x最小值。 分别从左向右和从右向左扫一遍最长递增子序列,然后枚举中点,求最大值max,那么n-max则为x最小值。 #include #include #include #include using namespace std;原创 2016-02-03 16:50:41 · 255 阅读 · 0 评论 -
POJ 3267 The Cow Lexicon(DP)
题目链接:http://poj.org/problem?id=3267 dp[i]表示从i到L所需要删除的最小字符数 如果不匹配:dp[i]=dp[i+1]+1; 如果匹配:dp[i]=min(dp[i],dp[pm]+(pm-i)-len); len为匹配的字符串的长度,pm为从i开始匹配字符串结束的下标,(pm-i)-len即为所需要删除的字符数 #include #include原创 2016-02-03 21:12:01 · 169 阅读 · 0 评论 -
poj 3311 Hie with the Pie(Floyd+状压dp)
传送门:http://poj.org/problem?id=3311 经典的旅行商问题。 dp[i][j]表示在i状态下到达j城市的最优解(i=1 dp[i][j]=min(dp[i][j],dp[i^(1 #include #include #include #include using namespace std; const int INF=0x3f3f3f3f; const原创 2016-02-15 21:38:06 · 194 阅读 · 0 评论 -
poj 3254 Corn Fields(状压DP+滚动数组)
传送门:http://poj.org/problem?id=3254 这题是状压DP的入门题吧。 用了滚动数组优化了一下,0msAC。 具体解释直接看代码: #include #include #include #include using namespace std; const int INF=0x3f3f3f3f; const int mod=100000000; const in原创 2016-02-15 09:28:29 · 259 阅读 · 0 评论 -
hdu 4512吉哥系列故事——完美队形I(动态规划LCIS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4512 RT,很容易发现这是一个最长公共递增子序列问题。 不过这道题数据很小,n只有200,理论上直接暴力应该是可以过的,未尝试。 很久没写动态规划了,手残调试了很久,具体见代码吧。。。 #include #include #include #include #include #incl原创 2016-07-14 16:49:09 · 317 阅读 · 0 评论