[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 ...
随机推荐
- 给pcm格式文件加wav文件头
#include <stdlib.h>#include <stdio.h>#include <string.h>void main(){ //wav头的结构如下所示 ...
- 邮件江湖群狼环伺 U-Mail邮件系统防狼有术
小时候听过一首儿歌<小兔子乖乖>,里面说到有条恶狼,常常冒充小兔子的“妈妈”,要求小兔 子开门,但小兔子谨守妈妈的训诫,就是不开门,直到辨别出妈妈在窗外的声音,才打开房门.如果我们将一些似 ...
- const实现
[const实现]
- Echarts tooltip文字没有左对齐
tooltip : { trigger: 'axis', axisPointer : { // 坐标轴指示器,坐标轴触发有效 type : 'shadow' // 默认为直线,可选为:'line' | ...
- scala 闭包的概念
闭包本质上是一个函数和其引用的变量的统一定义,它的返回值依赖于这个函数外部的一个或者多个变量. var test = (i:Int) => i+sum 这里 i是一个形参, 随着函数的调用传入不 ...
- jquery实现页面动态切换的方法--toggleClass(className)
$(function() { $(".A").click(function() { $(this).toggleClass("B"); }); }); 当点击带 ...
- (十一) 一起学 Unix 环境高级编程 (APUE) 之 高级 IO
. . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...
- JS向光标指定位置插入内容
方法: function insertHtmlAtCaret(html) { var sel, range; if (window.getSelection) { // IE9 and non-IE ...
- Java学习笔记15--引用传递
范例一 class Demo{ public int temp = 30; } public class T { public static void main(String[] args) { // ...
- Practice:输入年月日,判断该时间为一年的第几天
#-*- coding:utf- -*- ''' Created on -- # 输入年月日,判断为一年的第几天 @author: AdministrInputator ''' def leapYea ...