一、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.

二、题解

        这个题目怎么说呢,说难不难,说不难呢也花了我一天时间。刚开始的时候没什么思路,看到有讨论说是DP。但看了想了好久,也没找到状态转换方程,因为有不定长的X,Y的情况。在纠结了片刻后,我决定暴力解决,采用最简单的枚举方法,写出来我自己的惊呆了,O(N^6)。天啊,想了一下一定过不了。果然,TLE了。又想了几刻钟,看了下讨论,发现了一种不错的思路,把二维数组压缩成一维数组,然后再求最大子段和。这个压缩过程其实就是把第i+1行依次加到第i(0<= i <=n-1)行然后求最大子段和,记录最大值就OK了。



三、java代码

import java.util.Scanner;

 public class Main {
static int n; static int MaxSub (int a[], int N){
int max, i;
int[] dp=new int [n];
max = dp[0] = a[0];
for (i=1; i<N; i++){
if (dp[i-1] > 0)
dp[i] = dp[i-1] + a[i];
else
dp[i] = a[i];
if (dp[i] > max)
max = dp[i];
}
return max;
}
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
int i, j, k, Max, m;
n=cin.nextInt();
int[][] a =new int [n][n]; for (i=0; i<n; i++) {
for (j=0; j<n; j++){
a[i][j]=cin.nextInt();
}
} Max = Integer.MIN_VALUE;
for (i=0; i<n; i++){
m = MaxSub(a[i], n);
if (m > Max)
Max = m;
for (j=i+1; j<n; j++){
for (k=0; k<n; k++){
a[i][k] += a[j][k];
}
m = MaxSub(a[i], n);
if (m > Max)
Max = m;
}
}
System.out.print(Max);
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Poj1050_To the Max(二维数组最大字段和)的更多相关文章

  1. PHP 二维数组某个字段进行排序

    /** * @param $arrUsers * @return mixed *二维数组某个字段进行排序 */ function quick_sort($arrUsers) { $sort = arr ...

  2. PHP获取二维数组指定字段值的和

    array_sum(array_column($arr, 'num')); //获取二维数组 num字段的和 $arr = [ [ 'device_uid' => '123456', 'num' ...

  3. PHP 二维数组根据某个字段排序

    二维数组根据某个字段排序有两种办法,一种是通过sort自己写代码,一种是直接用array_multisort排序函数 一. 手写arraysort PHP的一维数组排序函数: sort  对数组的值按 ...

  4. php 将一个或多个二维数组组合成一个二维数组并根据某个字段排序排序

    最近再写项目的时候,碰到一个问题:如何将一个或多个二维数组组合成一个二维数组并根据某个字段排序排序:实在是想不到哪个php库中有哪个函数能实现,只能自己写一个了,将代码写出来后,发现自己的代码繁琐,并 ...

  5. php二维数组根据某个字段去重

    php的二维数组根据某个字段去重,在这默认为二维数组的结构是一样的,现在根据二维数组里的id字段去重,把id相同的重复的元素去掉 /** * 二维数组根据某个字段去重 * @param array $ ...

  6. PHP二维数组按某个字段排序

    //准备 二维数组 //按一个字段排序 foreach($rank as $key=>$val){ $dos[$key] = $val['timelength']; } array_multis ...

  7. PHP 二维数组根据某个字段按指定排序方式排序

    /** * 二维数组根据某个字段按指定排序方式排序 * @param $arr array 二维数组 * @param $field string 指定字段 * @param int $sort_or ...

  8. extract_by_one 根据二维数组中某字段来提取数组信息,查看有无重复信息

    public function tt(){ $param = array( array ( 'hykno' => '2222222-CB', 'tcdk_fid' => '458B6D70 ...

  9. PHP 二维数组根据某个字段排序 复制代码 array_multisort

    //二维数组,按照里面的age从大到小降序,代码如下 <?php header('Content-Type:text/html;Charset=utf-8'); $arrUsers = arra ...

随机推荐

  1. less (css预处理)

    用法 1. 必须在head内 2. 样式文件必须先加载 <head> <meta charset='utf-8'> <link rel="stylesheet/ ...

  2. Java基础 - 标识符

    标识符就是用来给包,类,方法变量等起名字的符号 组成规则: A:unicode字符 数字字符,英文大小写字母,汉字(不建议使用汉字) B:下划线 _ C:美元符 $ 注意事项: A:不能以数字开头 B ...

  3. 20179209《Linux内核原理与分析》第十二周作

    缓冲区溢出漏洞实验 缓冲区溢出简介 缓冲区溢出是指程序试图向缓冲区写入超出预分配固定长度数据的情况.这一漏洞可以被恶意用户利用来改变程序的流控制,甚至执行代码的任意片段.这一漏洞的出现是由于数据缓冲器 ...

  4. 基于SQLAIchemy的Flask目录

    预先知识 flask的基本使用 快速搭建开发的目录,以后我们在用Flask开发项目的时候可以直接用这个目录,不需要再自己创建. flask-sqlalchemy flask-sqlalchemy相当于 ...

  5. windows环境下JDK1.8安装

    jdk的安装,在Windows环境下没有什么特殊的选项,只需要“傻瓜式”安装就行(下一步). 如果说有的话,那就是你安装的路径,默认是c盘下的路径,可以根据自己的喜好,安装到自己的意愿目录: 当然,j ...

  6. SAP ATP邏輯可用性檢查

    [转http://tqmeng.blog.163.com/blog/static/169263916201162002414612/]SAP ATP邏輯可用性檢查1.可用性檢查群組OVZ2主要用於檢查 ...

  7. Navicat试用期破解方法(转)

    转载网址https://blog.csdn.net/Jason_Julie/article/details/82864187 1.按步骤安装Navicat Premium,如果没有可以去官网下载:ht ...

  8. || and && 理解

    逻辑或(||): 只要第一个值的布尔值为false,那么永远返回第二个值. 逻辑或属于短路操作,第一个值为true时,不再操作第二个值,且返回第一个值. 逻辑与(&&): 只要第一个值 ...

  9. Kindeditor API

    根据map规则删除range中的element或attribute. cmd.remove({ span : '*', div : 'class,border' });   commonNode(ma ...

  10. iOS 展示 gif

    gif 图 是多张依次有连续动作的图 顺时间展示的一种动态效果图 .   有的是均匀时间更换下一张  有的 则不是均匀时间变化 1. 那么 对于均匀 时间变化的gif图 比较适合 使用 iOS 系统自 ...