To the Max
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 45915   Accepted: 24282

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
 /*这道题目n^3居然不会超时,暴力就好了*/
#include<iostream>
using namespace std;
#include<cstdio>
#define N 101
int sum[N][N]={0},n,x;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
{
scanf("%d",&x);
sum[i][j]=sum[i][j-1]+x;/*sum[i][j]表示的是第i行前j个数的和*/
}
int ans=-(1<<30);
for(int i=1;i<=n;++i)
for(int j=i;j<=n;++j)/*暴力枚举每行区间*/
{
int tmp=0;/*tmp为当前矩阵的大小*/
for(int k=1;k<=n;++k)/*枚举每一行*/
{
int que=sum[k][j]-sum[k][i-1];/*取出这一行*/
if(tmp>0) tmp+=que;/*如果当前矩阵的大小已经<0了,那么再加上就要放弃之前的矩阵,放弃一定会更优*/
else tmp=que;
ans=max(ans,tmp);/*因为我们会随时放弃矩阵,所以最大值的更新,在循环中进行*/
}
}
cout<<ans;
return 0;
}

简单DP+暴力 POJ 1050的更多相关文章

  1. 【简单dp】 poj 2346

    题意:给定一个N 求一共有多少个N位数     前N/2个数的和等于后N/2个数的和思路:令F[i][j] 为sum值为j的i位数的个数则问题转化成 求 sum(F[n/2][j] * F[n/2][ ...

  2. HDU 4681 string 求最长公共子序列的简单DP+暴力枚举

    先预处理,用求最长公共子序列的DP顺着处理一遍,再逆着处理一遍. 再预处理串a和b中包含串c的子序列,当然,为了使这子序列尽可能短,会以c 串的第一个字符开始 ,c 串的最后一个字符结束 将这些起始位 ...

  3. 【简单dp】poj 2127 Greatest Common Increasing Subsequence【最长公共上升子序列】【模板】

    Sample Input 5 1 4 2 5 -12 4 -12 1 2 4 Sample Output 2 1 4 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列.输出最长的长度,并打表 ...

  4. 【简单dp】poj 1458 最长公共子序列【O(n^2)】【模板】

    最长公共子序列可以用在下面的问题时:给你一个字符串,请问最少还需要添加多少个字符就可以让它编程一个回文串? 解法:ans=strlen(原串)-LCS(原串,反串); Sample Input abc ...

  5. poj 1157 LITTLE SHOP_简单dp

    题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...

  6. 【POJ - 2533】Longest Ordered Subsequence (最长上升子序列 简单dp)

    Longest Ordered Subsequence 搬中文 Descriptions: 给出一个序列,求出这个序列的最长上升子序列. 序列A的上升子序列B定义如下: B为A的子序列 B为严格递增序 ...

  7. poj 1050 To the Max(最大子矩阵之和)

    http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here  也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...

  8. poj1189 简单dp

    http://poj.org/problem?id=1189 Description 有一个三角形木板,竖直立放.上面钉着n(n+1)/2颗钉子,还有(n+1)个格子(当n=5时如图1).每颗钉子和周 ...

  9. poj1163The Triangle(简单DP)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj ...

随机推荐

  1. HTML表单属性与全局属性

    1.全局属性

  2. 安装 Google BBR 加速VPS网络

    Google BBR就是谷歌公司提出的一个开源TCP拥塞控制的算法.详情可以看这儿:https://lwn.net/Articles/701165.https://blog.sometimesnaiv ...

  3. windows环境下搭建Redis集群

    转载请注明出处,原文章地址: https://www.cnblogs.com/tommy-huang/p/6240083.html Redis集群: 如果部署到多台电脑,就跟普通的集群一样:因为Red ...

  4. malloc原理和内存碎片【转】

    转自:http://www.cnblogs.com/zhaoyl/p/3820852.html 当一个进程发生缺页中断的时候,进程会陷入内核态,执行以下操作: 1.检查要访问的虚拟地址是否合法 2.查 ...

  5. python实战===python程序打包成exe

    推荐PyInstaller项目www.pyinstaller.org   安装方法: 先跑pip install pywin32再跑pip install pyinstaller即可 可用一句命令打包 ...

  6. Linux删除除了今天以外的文件

    [背景] 开发到日志记录功能时,每天都会产生当天的一个日志,久而久之就会产生累积,想要查看的时候,tab键无法自动补全,还要自己额外输入. 比较麻烦. [命令] 经过查找和实验,找到了以下的方法: 1 ...

  7. BootStrap的table表格,栅格系统,form表单的样式

    BootStrap BootStrap的简介 1.    什么是Bootstrap 由两个前端设计师开发的一个前端的框架(Html,css,js) 简化了程序员写css的代码 2.    为什么使用B ...

  8. 窗口生效函数UpdateData

    Invalidate()使整个窗口客户区无效.窗口的客户区无效意味着需要重绘,例如,如果一个被其它窗口遮住的窗口变成了前台窗口,那么原来被遮住的部分就是无效的,需要重绘.这时Windows会在应用程序 ...

  9. 二、python框架相关知识体系

    Django框架 1.django框架.flask框架和Tornado框架的区别? django框架,内置组件多,自身功能强大,是一个大而全的框架,ORM.Admin.中间件.Form.ModelFr ...

  10. 利用Fidder工具抓取App数据包

    第一步:下载神器Fiddler,下载链接: http://fiddler2.com/get-fiddler 下载完成之后,傻瓜式的安装一下了! 第二步:设置Fiddler打开Fiddler,     ...