[ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)
Description
As an example, the maximal sub-rectangle of the array:
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
is in the lower left corner:
9 2
-4 1
-1 8
and has a sum of 15.
Input
Output
Sample Input
4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1 8 0 -2
Sample Output
15
Source
题目大意:给一个n*n的整数矩阵,找出一个子矩阵使其和最大。
解题思路:• 该题其实就是最大连续和问题在二维空间上的推广。
• 先来看一下一维的最大连续和问题:
♣ 给出一个长度为n的序列A1,A2,A3.....An,求一个连续子序列Ai,Ai+1,....Aj使得元素总和最大。
♥ 我们以temp[i]表示以Ai结尾的子段中的最大子段和。在已知temp[i]的情况下,求temp [i+1]的方法是:
如果temp[i]>0 temp [i+1]= temp[i]+ai(继续在前一个子段上加上ai),否则temp[i+1]=ai(不加上前面的子段);
也就是说 状态转移方程:temp[i] = (temp[i-1]>0?temp[i-1]:0)+buf[i];
int getMax(int buf[],int n){
|
• 对于本题可以暴力枚举i到j行,针对每一个i到j行的一列元素求和就将i到j行的2维情况转化为1维情况:如:
0 -2 -7 0
9 2 -6 2
-4 1 -4 7
-1 8 0 -2
取i=2,j=4,压缩为4(9 -4 -1),11(2 1 8),-10(-6 -4 0),7(2 7 -2)新的一维buf[]={4,11,-10,7},
然后求出buf[]的最大连续和就是2到4行范围内的最大矩阵的值。这样2层循环暴力所有i到j的情况取最大值即可!
#include<iostream>
using namespace std;
int rect[][];//2维矩阵
int n,Max;;
int buf[];//中间1维矩阵
int getMax(){
int Temp[],max=n*(-);
memset(Temp,,sizeof(Temp));
for(int i=;i<=n;i++){
Temp[i]=(Temp[i-]> ? Temp[i-] : )+buf[i];
if(max<Temp[i])
max=Temp[i];
}
return max;
}//求最大连续和
void read(){
for(int i=;i<n;i++)
for(int j=;j<n;j++)
cin>>rect[i][j];
}//读入
int solve(){
Max=-*n;
for(int i=;i<n;i++){
for(int j=i;j<n;j++){//2层循环暴力所有i到j组合
memset(buf,,sizeof(buf));//压缩,2维变1维
for(int k=;k<n;k++)
for(int L=i;L<=j;L++)
buf[k]+=rect[k][L];
int d=getMax();//获得最大连续和
if(d>Max)Max=d;//更新Max值
}
}
return Max;
}//2维变1维暴力
int main(){
while(cin>>n){
read();
solve();
cout<<Max<<'\n';
}return ;
}
[ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)的更多相关文章
- POJ 1050 To the Max -- 动态规划
题目地址:http://poj.org/problem?id=1050 Description Given a two-dimensional array of positive and negati ...
- POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)
传送门: http://poj.org/problem?id=1050 To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- poj - 1050 - To the Max(dp)
题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 ...
- poj 1050 To the Max(线性dp)
题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...
- poj 1050 To the Max(最大子矩阵之和)
http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here 也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...
- POJ 1050 To the Max (最大子矩阵和)
题目链接 题意:给定N*N的矩阵,求该矩阵中和最大的子矩阵的和. 题解:把二维转化成一维,算下就好了. #include <cstdio> #include <cstring> ...
- POJ 1050 To the Max 二维最大子段和
To the MaxTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 52281 Accepted: 27633Description ...
- [poj]1050 To the Max dp
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- POJ 1050 To the Max 暴力,基础知识 难度:0
http://poj.org/problem?id=1050 设sum[i][j]为从(1,1)到(i,j)的矩形中所有数字之和 首先处理出sum[i][j],此时左上角为(x1,y1),右下角为(x ...
随机推荐
- UVM Top Testbench
top testbench在top_tb中包含进所有的文件,将DUT放在top_tb中(例化DUT),连接好各个端口,提供clk时钟和rst_n复位信号.最主要的是要给组件中的虚接口设置接口,一般是给 ...
- WinExec
WinAPI: WinExec - 运行外部程序 //声明 WinExec( lpCmdLine: LPCSTR; {文件名和参数; 如没指定路径会按以下顺序查找: 程序目录/当前目录/Syste ...
- 深刻理解和运用XMLHttpRequest
本文为转载文章,因见猎心喜,担心失传,故贴此以备不时之需. 原文地址:传送 你真的会使用XMLHttpRequest吗? xmlhttprequest http cors ajax ruoyiqing ...
- 纯手工打造(不使用IDE)java web 项目
必备环境 1.编译器:jdk 2.web服务器:tomcat 3.文本编辑器:sublime,编写java文件和jsp文件,没有的话用记事本也行. 一.建立工程目录结构,如下图 在操作系统下完成即可, ...
- PTA List Components
For a given undirected graph with N vertices and E edges, please list all the connected components b ...
- (转) Lua使用心得一 LUA和VC整合
这几天研究了一下lua,主要关注的是lua和vc之间的整合,把代码都写好放在VC宿主程序里,然后在lua里调用宿主程序的这些代码(或者叫接口.组件,随便你怎么叫),希望能用脚本来控制主程序的行为.这实 ...
- Linux C _exit函数与exit函数的联系与区别
一.联系 1.功能上,_exit和exit函数都是让进程正常退出,即关闭进程所打开的文件描述符,释放已占用内存和其他资源. 二.区别 1._exit函数在头文件unistd.h中声明,而exit在头文 ...
- 第三章 springboot + jedisCluster
如果使用的是redis2.x,在项目中使用客户端分片(Shard)机制.(具体使用方式:第九章 企业项目开发--分布式缓存Redis(1) 第十章 企业项目开发--分布式缓存Redis(2)) 如果 ...
- R语言--数据预处理
一.日期时间.字符串的处理 日期 Date: 日期类,年与日 POSIXct: 日期时间类,精确到秒,用数字表示 POSIXlt: 日期时间类,精确到秒,用列表表示 Sys.date(), date( ...
- 编写出色的GNU/Linux程序
http://advancedlinuxprogramming.com提供了本书电子版的免费下载. 1 与执行环境交互 关于参数 C语言程序的main()函数使用两个参数和执行环境交互--(int)a ...