一、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. 监控httpd的shell脚本

    #!/bin/bash # Apache httpd进程监控shell # 如果你在使用RHEL / CentOS / Fedora Linux,使用这个重启命令 RESTART="/sbi ...

  2. 基于flask的web微信

    web微信 1.扫码获取头像 当你打开web微信的时候,因为http是无状态的,web微信如何实时的获取用户的扫码动作? 那么这里用到的是长轮询的方式. from flask import Flask ...

  3. (转)关于Http协议,一片就够了

    HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送 ...

  4. 基于Web的实验室管理系统技术简要报告

    基于Web的实验室管理系统技术简要报告 Copyright 朱向洋 Sunsea ALL Right Reserved 一.网站架构 该网站使用C#语言,利用SQL Server2008数据库,采用V ...

  5. [原创]java WEB学习笔记12:一个简单的serlet连接数据库实验

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  6. Quartz.Net在C#中的使用

    概述 Quartz.NET是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET允许开发人员根据时间间隔(或天)来调度作业.它实现了 ...

  7. Spring Cloud之Hystrix服务保护框架

    服务保护利器 微服务高可用技术 大型复杂的分布式系统中,高可用相关的技术架构非常重要. 高可用架构非常重要的一个环节,就是如何将分布式系统中的各个服务打造成高可用的服务,从而足以应对分布式系统环境中的 ...

  8. linux shell编程(三) if 和 for

    if 条件判断: 单分支的if语句if 判断条件: then statement1fi双分支的if语句if 判断条件;then statement1 statementelse statement3f ...

  9. centos虚拟机启用网卡

    CentOS虚拟机安装成功后,默认开机未启用网关,通过修改配置文件,启用网卡 编辑系统配置文件,虚拟机完成后,系统安装了一个默认的网卡,即eth0,其配置文件的路径为/etc/sysconfig/ne ...

  10. UniDAC 安装教程

    翻译: 1.解压后把UniDAC文件夹直接复制到你专门用来存放第三方控件的地方(这一步根据自己的喜好,可以跳过这一步)2.在UniDAC\Source\Delphi21文件夹中找到Make.bat文件 ...