一、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. 九度OJ 1180:对称矩阵 (矩阵计算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2637 解决:1354 题目描述: 输入一个N维矩阵,判断是否对称. 输入: 输入第一行包括一个数:N(1<=N<=100),表 ...

  2. 启动/关闭Spring boot服务脚本

    启动Spring boot服务脚本 #!/bin/bash cd /test java -jar test.jar &> ./test.log & echo "成功&q ...

  3. python爬虫之Selenium

    Selenium的使用 #!/usr/bin/env python # -*- coding:utf-8 -*- """ Selenium是一个第三方模块,可以完全模拟用 ...

  4. postgres=# psql -U postgres -h 127.0.0.1 -p 5432 -d dreamstart_dev -w

    postgres=# psql -U postgres -h 127.0.0.1 -p 5432 -d dreamstart_dev -wpostgres-# \dNo relations found ...

  5. surf算法解析

    surf构造的金字塔图像与sift有很大的不同,sift采用的是DOG图像,surf采用的是hessian矩阵行列式近似值图像,hessian矩阵是surf算法的核心,构建hessian矩阵的目的是为 ...

  6. 什么是gevent

    gevent是一个基于协程的python网络库,它使用greenlet在libev或libuv事件循环之上提供高级同步API 功能包括 基于libev或libuv的快速时间循环 基于greenlets ...

  7. eclipse---个人设置

    window---- preferences -----修改背景颜色 -----修改字体 ----修改窗口主题 ----设置编码 -----设置编译环境 ----设置web项目JDK编译的版本 --- ...

  8. [原创]java WEB学习笔记13:JSP介绍(背景,特点,原理)

    JSP介绍:(理解) 1)JSP背景 ①在很多动态网页中,绝大部分内容都是固定不变的,只有局部内容需要动态产生和改变: ②如果使用Servlet程序来输出只有局部内容需要动态改变的网页,其中所有的静态 ...

  9. vary的用法

    对于vary的用法,网上有许多种说法,云里雾里的,在此仅阐述一下本人的一些理解,首先是官方解释: Vary头域值指定了一些请求头域,这些请求头域用来决定: 当缓存中存在一个响应,并且该缓存没有过期失效 ...

  10. curl使用手册

    查询curl耗时 curl -o /dev/null -s -w %{time_namelookup}::%{time_connect}::%{time_starttransfer}::%{time_ ...