poj 1050 To the Max(最大子矩阵之和,基础DP题)
To the Max
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 38573
Accepted: 20350
Description
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.
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
The input consists of an N * N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N^2 integers separated by whitespace (spaces and newlines). These are the N^2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].
Output
Output the sum of the maximal sub-rectangle.
Sample Input
4
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
Sample Output
15
题解:假设已经知道矩形的上下边界,比如知道矩形的区域的上下边界分别是第a行和第c行,现在要确定左右边界;
代码:
#include <iostream>
#define INF 2147483647
using namespace std;
int a[1010][1010];
int sum[1010][1010];//数组我开的比较大,这无所谓 int Maxsum(int n,int m)
{//求最大子矩阵之和
int i,j,k;
int Max=-INF;
for(i=0;i<=n;i++)
sum[i][0]=0;
for(i=1;i<=m;i++)
sum[0][i]=0;
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
sum[j][m]=sum[j-1][m]+a[j][m];//sum[a][b]储存 第b列中 第1行到第a行之间所有元素的和
int tmp=sum[j][m]-sum[i-1][m];//此时tmp值为 第m列中 第i行到第j行之间所有元素之和
int big=tmp;
for(k=m-1;k>=1;k--)
{
if(tmp<0)
tmp=0;
sum[j][k]=sum[j-1][k]+a[j][k];
tmp+=sum[j][k]-sum[i-1][k];
big=max(big,tmp);
Max=max(big,Max);
}
}
}
return Max;
} int main()
{
int n,m,i,j;
cin>>n;
m=n;//这里可变为cin>>m,则矩阵是n*m
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
cin>>a[i][j];
cout<<Maxsum(n,m)<<endl;
return 0;
}
poj 1050 To the Max(最大子矩阵之和,基础DP题)的更多相关文章
- poj 1050 To the Max(最大子矩阵之和)
http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here 也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...
- poj 1050 To the Max 最大子矩阵和 经典dp
To the Max Description Given a two-dimensional array of positive and negative integers, a sub-rect ...
- 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 (最大子矩阵和)
题目链接 题意:给定N*N的矩阵,求该矩阵中和最大的子矩阵的和. 题解:把二维转化成一维,算下就好了. #include <cstdio> #include <cstring> ...
- hdu 1081 & poj 1050 To The Max(最大和的子矩阵)
转载请注明出处:http://blog.csdn.net/u012860063 Description Given a two-dimensional array of positive and ne ...
- [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)
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 ...
- 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(dp)
题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 ...
随机推荐
- asp.net MVC ajax上传文件
普通上传 view: <body> <form id="form1" method="post" action="@Url.Acti ...
- 【C#进阶系列】03 配置文件管理与程序集的引用版本重定向
先来点与标题不相关的: CLR支持两种程序集:弱命名程序集和强命名程序集. 两者的区别在于强命名程序集使用发布者的公钥和私钥进行签名.由于程序集被唯一性地标识,所以当应用程序绑定到强命名程序集时,CL ...
- *nix下传统编程入门之GCC
准备工作 注意:本文可能会让你失望,如果你有下列疑问的话:为什么要在终端输命令啊? GCC 是什么东西,怎么在菜单中找不到? GCC 不能有像 VC 那样的窗口吗?…… 那么你真正想要了解的可能是 a ...
- WinJs项目介绍
WinJs库是最近微软公布的一个开源项目.它与开源社区的协作共同完成.为了轻易创建HTML/JS/CSS应用程序开发的解决方案.WinJS是一个Javascripts的工具箱让开发人员使用HT ...
- Asp.net发布的CheckList
Asp.net Web 应用程序正式发布前,我们还是做一些检查,所以需要这个CheckList,如下图今天的Asp.net 已演化这样的了: 但不管是什么组件,目前的Web最终还得通过H ...
- H5调用Android拨打电话
1.AndroidAndJSInterface.java class AndroidAndJSInterface { /** * 该方法将被js调用,用于加载数据 */ @JavascriptInte ...
- 我所了解的WEB开发(3) - 彩虹的颜色
据说彩虹有七彩颜色,从外至内分别为:红.橙.黄.绿.青.蓝.紫.这些我倒是没有验证过,但是学生时代就不止一次色盲检测,还是让我足够确信对颜色的分辨应该和大多数人相似的. 还听说大多数哺乳动物是色盲.如 ...
- Echarts图表控件使用总结2(Line,Bar)—问题篇
Echarts图表控件使用总结1(Line,Bar):http://www.cnblogs.com/hanyinglong/p/Echarts.html 1.前言 a.前两天简单写了一篇在MVC中如何 ...
- Atitit.木马病毒的免杀原理---sikuli 的使用
Atitit.木马病毒的免杀原理---sikuli 的使用 1. 使用sikuli java api1 1.1. 3. Write code!1 2. 常用api2 2.1. wait 等待某个界面出 ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q51-Q53)
Question 51You use a third-party site definition to create SharePoint sites.You need to add a Web Pa ...