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. D - Binary Lexicographic Sequence URAL - 1081 (贪心)

    题目链接:https://cn.vjudge.net/contest/275079#problem/D 具体思路:首先,我们可以观察到1-n位数的种数连起来是一个很有规律的数列,然后我们开始倒序建立. ...

  2. Python os.path.dirname(__file__) os.path.join(str,str)

    Python os.path.dirname(__file__) Python os.path.join(str,str)   (1).当"print os.path.dirname(__f ...

  3. Axure RP 授权码

    Axure RP 8.1.0.3372Licensee:KoshyKey:wTADPqxn3KChzJxLmUr5jTTitCgsfRkftQQ1yIG9HmK83MYSm7GPxLREGn+Ii6x ...

  4. ubuntu14.04安装使用NviDIA显卡驱动

    想给自己的ubuntu换N卡驱动的原因: 一方面,由于自己电脑在编译源代码8线程全开(make -j8)时,CPU温度呼呼涨到八九十度,从而常常导致系统保护自动关机,让人有点不爽.网上有说ubuntu ...

  5. 003iptables 命令介绍

    http://www.cnblogs.com/wangkangluo1/archive/2012/04/19/2457072.html iptables 防火墙可以用于创建过滤(filter)与NAT ...

  6. StringBuilder基本用法

    //StringBuilder用法 public class StringBuilderTest { public static void main(String[] args) { StringBu ...

  7. java基础16 捕获、抛出以、自定义异常和 finally 块(以及关键字:throw 、throws)

    1.异常的体系 /* ------|Throwable:所有异常和错误的超类 ----------|Error(错误):错误一般用于jvm或者硬件引发的问题,所以我们一般不会通过代码去处理错误的 -- ...

  8. PHP学习笔记之数组游标操作

    数组有N个单元,同时只能操作一个单元.比如循环时,只能一个一个单元读取他的值. 那么数组是怎么记住刚才读取的是哪个单元,接着读取下个单元的呢? 在数组内部,有一个指针,指针指向某一个单元. 每循环一个 ...

  9. 手游研发CJ抱大腿指南

    文摘要:CJ来了,又是一年一度的游戏圈盛事,随着手游行业的迅速崛起,今年CJ上,手游研发商以及发行商必定成为焦点.由于门槛低.市场热.前景好等因素的影响,国内一下子蹦出一大堆手游研发团队.很幸运(或者 ...

  10. leetcode 之Remove Duplicates from Sorted Array(1)

    删除数组中的重复元素并返回新数组的个数 思路:保留不同的元素即可. int removeDeplicates(int A[], int n) { ; ; i < n; i++) { if (A[ ...