算法讲解 -- 区间dp经典模型与优化(石子归并)

石子合并问题是最经典的DP问题。首先它有如下3种题型:

PPT讲解:点击打开链接

(1)有N堆石子,现要将石子有序的合并成一堆,规定如下:每次只能移动任意的2堆石子合并,合并花费为新合成的一堆石子的数量。求将这N堆石子合并成一堆的总花费最小(或最大)。

 

分析:当然这种情况是最简单的情况,合并的是任意两堆,直接贪心即可,每次选择最小的两堆合并。本问题实际上就是霍夫曼的变形。

例题链接:点击打开链接

 

(2)有N堆石子,现要将石子有序的合并成一堆,规定如下:每次只能移动相邻的2堆石子合并,合并花费为新合成的一堆石子的数量。求将这N堆石子合并成一堆的总花费最小(或最大)。

 

 

分析:我们熟悉矩阵连乘,知道矩阵连乘也是每次合并相邻的两个矩阵,那么石子合并可以用矩阵连乘的方式来解决。

 

设dp[i][j]表示第i到第j堆石子合并的最优值,sum[i][j]表示第i到第j堆石子的总数量。那么就有状态转移公式:

 

 

 

#include <bits/stdc++.h>
#define pr(x) cout << #x << "= " << x << "  "
#define pl(x) cout << #x << "= " << x << endl;
#define Memset(x, a) memset(x, a, sizeof(x))
#define ll __int64
using  namespace  std;const int inf=0x3f3f3f3f;
const int N=205;
int a[N];
int sum[N];
int dp[N][N];int getans(int a[],int n){for(int i=0; i<n; i++){dp[i][i]=0;}for(int v=1; v<n; v++){//i,j之间的间距for(int i=0; i<n-v; i++){int j=i+v;dp[i][j]=inf;int tmp=sum[j]-(i>0?sum[i-1]:0);for(int k=i; k<j; k++)dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+tmp);}}return  dp[0][n-1];
}int  main(){int  n;while(~scanf("%d",&n)){for(int i=0; i<n; i++){scanf("%d",&a[i]);}sum[0]=a[0];for(int  i=1; i<n; i++){sum[i]=sum[i-1]+a[i];}printf("%d\n",getans(a,n));}return 0;
}

 

 

直线取石子问题的平行四边形优化(用一个p【i】【j】=k 表示区间 i---j 从k点分开才是最优的,这样的话我们就可以优化掉一层复杂度,变为O(n^2) 

#include <bits/stdc++.h>
using namespace std;const int inf = 0x3f3f3f3f;
const int N = 1005;int dp[N][N];
int p[N][N];
int sum[N];
int n;int getans(){for(int i=1; i<=n; i++){dp[i][i] = 0;p[i][i] = i;}for(int len=1; len<n; len++){for(int i=1; i+len<=n; i++){int end = i+len;int tmp = inf;int k = 0;for(int j=p[i][end-1]; j<=p[i+1][end]; j++){if(dp[i][j] + dp[j+1][end] + sum[end] - sum[i-1] < tmp){tmp = dp[i][j] + dp[j+1][end] + sum[end] - sum[i-1];k = j;}}dp[i][end] = tmp;p[i][end] = k;}}return dp[1][n];
}int main()
{while(scanf("%d",&n)!=EOF){sum[0] = 0;for(int i=1; i<=n; i++){int val;scanf("%d",&val);sum[i] = sum[i-1] + val;}printf("%d\n",getans());}return 0;
}

 

(3)问题(2)的是在石子排列是直线情况下的解法,如果把石子改为环形排列,又怎么做呢?

 

 

分析:状态转移方程为:

 

 

 

 

其中有:

 

#include <iostream>
#include <string.h>
#include <stdio.h>using namespace std;
const int INF = 1 << 30;
const int N = 205;int mins[N][N];
int maxs[N][N];
int sum[N],a[N];
int minval,maxval;
int n;int getsum(int i,int j)
{if(i+j >= n) return getsum(i,n-i-1) + getsum(0,(i+j)%n);else return sum[i+j] - (i>0 ? sum[i-1]:0);
}void Work(int a[],int n)
{for(int i=0;i<n;i++)mins[i][0] = maxs[i][0] = 0;for(int j=1;j<n;j++){for(int i=0;i<n;i++){mins[i][j] = INF;maxs[i][j] = 0;for(int k=0;k<j;k++){mins[i][j] = min(mins[i][j],mins[i][k] + mins[(i+k+1)%n][j-k-1] + getsum(i,j));maxs[i][j] = max(maxs[i][j],maxs[i][k] + maxs[(i+k+1)%n][j-k-1] + getsum(i,j));}}}minval = mins[0][n-1];maxval = maxs[0][n-1];for(int i=0;i<n;i++){minval = min(minval,mins[i][n-1]);maxval = max(maxval,maxs[i][n-1]);}
}int main()
{while(scanf("%d",&n)!=EOF){for(int i=0;i<n;i++)scanf("%d",&a[i]);sum[0] = a[0];for(int i=1;i<n;i++)sum[i] = sum[i-1] + a[i];Work(a,n);printf("%d %d\n",minval,maxval);}return 0;
}

可以看出,上面的(1)(3)问题的时间复杂度都是O(n^3),由于过程满足平行四边形法则,故可以进一步优化到O(n^2)。

转自这里

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441061.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

cpu影响matlab仿真速度吗,Proteus仿真速度很慢的分析

类型&#xff1a;行业软件大小&#xff1a;1.1M语言&#xff1a;中文 评分&#xff1a;6.5标签&#xff1a;立即下载这篇文章是我的个人实践经验&#xff1a;很多朋友在做Proteus硬件仿真的时候可能都碰上了仿真速度慢的问题&#xff0c;在点击了开始仿真之后&#xff0c;CPU过…

【HDU - 5900】QSC and Master(区间dp)

题干&#xff1a; Every school has some legends, Northeastern University is the same. Enter from the north gate of Northeastern University&#xff0c;You are facing the main building of Northeastern University.Ninety-nine percent of the students have not …

php set_timeout,和 JS 一样的 php setTimeout 函数

112016-07-11 21:34:11 08:00szopen 貌似没有复杂啊//enable ticksdeclare (ticks 1);//setTimeout event list$timeoutQueue new SplObjectStorage;register_tick_function(function() {global $timeoutQueue;foreach ($timeoutQueue as $v) {$callBack $v();if (is_callab…

mysql 7天自动拒单功能,mysql查询最近7天的数据,没有数据自动补0

select DATE( createtime) date , createtime, count(1) as count from order表 where DATEDIFF( now(), createtime)<7group by date ;12 月九号没有;给他在关联一张日期的表;--生成从今天开始完整的7天日期DECLARE LastSevenDaytable(day date)DECLARE StartDaydate …

【UVA - 10038】Jolly Jumpers (模拟,水题,标记)

题干&#xff1a; 题目大意&#xff1a; 要任意相邻的两个数的绝对值在[1,n)&#xff0c;而且这个范围内的每个数都要出现一次。 解题报告&#xff1a; 直接模拟就行了、 AC代码&#xff1a; #include<cstdio> #include<iostream> #include<algorithm> …

基于维纳滤波的语音增强算法 matlab,基于维纳滤波语音增强算法的改进实现

通过对维纳滤波的介绍,实现了基本维纳滤波效果;利用两级维纳滤波和两级滤波器组滤波方法实现了语音增强,达到了良好的效果。维普资讯 http://doc.docsou.com文章编号&#xff1a;0 2 8 8 (o 7 0 - 0 4 0 10—6 4 2 o )1 0 4 - 3基于维纳滤波语音增强算法的改进实现白文雅&#…

【CodeForces - 245H 】Queries for Number of Palindromes (带容斥的区间dp)

题干&#xff1a; Youve got a string s  s1s2... s|s| of length |s|, consisting of lowercase English letters. There also are q queries, each query is described by two integers li, ri (1 ≤ li ≤ ri ≤ |s|). The answer to the query is the number of …

php7 获取文件类型,太简单了!PHP获取文件扩展名的7中方法

PHP中获取文件扩展名的方法第一种&#xff1a;$file x.y.z.png;echo substr(strrchr($file, .), 1);解析&#xff1a;strrchr($file, .)strrchr() 函数查找字符串在另一个字符串中最后一次出现的位置&#xff0c;并返回从该位置到字符串结尾的所有字符第二种&#xff1a;$file…

【EOJ Monthly 2018.12 - A,B,C】套题训练,部分题解

A. 题干&#xff1a; A. 仰望星空 单测试点时限: 2.0 秒 内存限制: 512 MB 你就这样静坐在草地上&#xff0c;离我稍远的地方。 我用眼角瞅着你&#xff0c;你什么话也别说。 语言是误会的根源。 但是&#xff0c;每天&#xff0c;你可以坐得离我近一些…… 你和她一起仰头…

php android 复制粘贴板,Android_Android剪贴板用法详解,本文实例详述了Android剪贴板的 - phpStudy...

Android剪贴板用法详解本文实例详述了Android剪贴板的用法&#xff0c;分享给大家供大家参考。具体方法分析如下&#xff1a;这里首先需要注意的一点&#xff0c;就是在使用Android剪贴板的时候大家只记住一点就行了&#xff0c;不管是安卓设备还是PC机&#xff0c;复制粘贴在同…

【Uva - 10047 】The Monocycle(搜索,bfs记录状态)

题干&#xff1a; Uva的题目就不粘贴题干了&#xff0c;&#xff0c;直接上题意吧。 有你一个独轮车&#xff0c;车轮有5种颜色&#xff0c;为了5中颜色的相对位置是确定的。有两种操作&#xff1a;1.滚动&#xff1a;轮子必须沿着顺时针方向滚动&#xff0c;每滚动一次会到达…

matlab中bwlabel意思,Matlab 里bwlabel 函数的具体含义

Matlab函数bwlabel&#xff1a;在二值图像中标记连通区域用法&#xff1a;L bwlabel(BW,n)返回一个和BW大小相同的L矩阵&#xff0c;包含了标记了BW中每个连通区域的类别标签&#xff0c;这些标签的值为1、2、num(连通区域的个数)。n的值为4或8&#xff0c;表示是按4连通寻找区…

php百度搜索框代码,基于jquery的仿百度搜索框效果代码_jquery

先看看整个的效果图&#xff1a;图一&#xff1a;图二&#xff1a;图三&#xff1a;图四&#xff1a;大概的效果图就这样&#xff0c;接下来直接看源码页面&#xff1a;CSS&#xff1a;.autoSearchText{border:solid 1px #CFCFCF;height:20px;color:Gray;}.menu_v{margin:0;pad…

【UVA - 227】Puzzle (模拟,水题)

题干&#xff1a; Puzzle A childrens puzzle that was popular 30 years ago consisted of a 5x5 frame which contained 24 small squares of equal size. A unique letter of the alphabet was printed on each small square. Since there were only 24 squares within the…

php 解析mib文件,Mib库解析

MibAnalyser介绍MibAnalyser可以解析MIB文件&#xff0c;并转化为对应的实体&#xff0c;持久化到本地。MibAnalyser分为三个模块&#xff1a;解析模块、持久化模块、工具库模块。解析模块解析模块用于解析MIB文件的语法&#xff0c;并最终生成实体列表。管理模块由于对MIB文件…

【CodeForces - 299C 】Weird Game (思维,模拟,贪心,博弈,OAE思想)

题干&#xff1a; Yaroslav, Andrey and Roman can play cubes for hours and hours. But the game is for three, so when Roman doesnt show up, Yaroslav and Andrey play another game. Roman leaves a word for each of them. Each word consists of 2n binary characte…

matlab大作业题题单,2011MATLAB大作业-题目-

(1)求解线性规划问题&#xff1a;minZ 4x1 x2 7x3s.t.x1 x2 x3 53x1 x2 x3 4x1 x2 4x3 7x1,x2 0问各xi分别取何值时&#xff0c;Z有何极小值。(2)编写一个函数&#xff0c;使其能够产生如下的分段函数&#xff1a;0.5x&#xff0c;x 2f(x) 1.5 0.25x&#xff0c;2 x 6&#xff…

【CodeForces - 298D】Fish Weight (OAE思想,思维)

题干&#xff1a; It is known that there are k fish species in the polar ocean, numbered from 1 to k. They are sorted by non-decreasing order of their weight, which is a positive number. Let the weight of the i-th type of fish be wi, then 0 < w1 ≤ …

php 字符串比较的规则,PHP字符串比较函数strcmp()与strcasecmp()的用法介绍

使用“”来判断。它和“”的区别&#xff0c;前者强调“identical(相同的&#xff0c;完全相同)”类型也要求一样&#xff1b;后者要求“equal(相等)”&#xff0c;值相同就可以了。或者使用strcmp来判断&#xff0c;但是这不能说明两个字符串是否相等。一般能用 !, 比较两个对…

【CodeForces - 140C】New Year Snowmen (贪心)

题干&#xff1a; As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergeys twins help him: theyve already made n sno…