To the Max
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 39081   Accepted: 20639

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
 /*
* File: main.cpp
* Author: liaoyu <liaoyu@whu.edu.cn>
*
* Created on April 1, 2014, 5:34 PM
*/ #include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cmath>
#include <algorithm>
using namespace std; int a[][]; int maxSubArray(int n, int* a)
{
int max = 0x80000000;
int b = a[];
for (int i = ; i < n; i++) {
if (b > )b += a[i];
else b = a[i];
if (b > max)max = b;
}
return max;
}
int b[]; int main()
{
int n;
scanf("%d", &n);
int max = 0x80000000;
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
scanf("%d", &a[i][j]);
for (int i = ; i < n; i++)
for (int j = i; j < n; j++) {
for (int l = ; l < n; l++) {
b[l] = ;
for (int k = i; k <= j; k++) {
b[l] += a[l][k];
}
}
int tmp = maxSubArray(n, b);
if (tmp > max)max = tmp;
}
printf("%d\n", max);
}

poj1050的更多相关文章

  1. 【poj1050】 To the Max

    http://poj.org/problem?id=1050 (题目链接) 题意 求二维最大子矩阵 Solution 数据好像很水,N最大才100,N^4大暴力都可以随便水过. 其实有N^3的做法.枚 ...

  2. [POJ1050]To the Max

    [POJ1050]To the Max 试题描述 Given a two-dimensional array of positive and negative integers, a sub-rect ...

  3. POJ1050:To the max

    poj1050:http://poj.org/problem?id=1050 * maximum-subarray 问题的升级版本~ 本题同样是采用DP思想来做,同时有个小技巧处理:就是把二维数组看做 ...

  4. [POJ1050] To the Max 及最大子段和与最大矩阵和的求解方法

    最大子段和 Ο(n) 的时间求出价值最大的子段 #include<cstdio> #include<iostream> using namespace std; int n,m ...

  5. [POJ1050]To the Max (矩阵,最大连续子序列和)

    数据弱,暴力过 题意 N^N的矩阵,求最大子矩阵和 思路 悬线?不需要.暴力+前缀和过 代码 //poj1050 //n^4暴力 #include<algorithm> #include& ...

  6. DP----入门的一些题目(POJ1088 POJ1163 POJ1050)

    动态规划入门 DP 基本思想 具体实现 经典题目 POJ1088 POJ1163 POJ1050 (一) POJ1088,动态规划的入门级题目.嘿嘿,连题目描述都是难得一见的中文. 题目分析: 求最长 ...

  7. POJ1050 To the Max 最大子矩阵

    POJ1050 给定一个矩阵,求和最大的子矩阵. 将每一列的值进行累加,枚举起始行和结束行,然后就可以线性优化了 复杂度O(n^3) #include<cstdio> #include&l ...

  8. [POJ1050]To the Max(最大子矩阵,DP)

    题目链接:http://poj.org/problem?id=1050 发现这个题没有写过题解,现在补上吧,思路挺经典的. 思路就是枚举所有的连续的连续的行,比如1 2 3 4 12 23 34 45 ...

  9. poj1050(nyoj104 zoj1074)dp问题

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39913   Accepted: 21099 Desc ...

随机推荐

  1. Lab_2_SysOps_VPC_Linux_v2.5

    System Operations - Lab 2: Configuring a Virtual Private Cloud with Amazon VPC - Linux - 2.5 ======= ...

  2. PHP简介

    PHP PHP是运行在服务器端的脚本,可以运行在UNIX.LINUX.WINDOWS.Mac OS下.PHP的特性包括:1. PHP 独特的语法混合了 C.Java.Perl 以及 PHP 自创新的语 ...

  3. <<软件测试实战>>读书笔记

    软件测试基础 软件的复杂度已经超越了人的理解能力 1. 虽然高抽象的层次语言,程序框架,程序库等提高了人的生产力,但是还是需要开发者深入理解细节,可以减少开发时间,但是无法减少开发者学习整个技术栈的时 ...

  4. 【linux】之相关命令

    防火墙 ) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off ) 即时生效,重启后失效 开启: service iptables s ...

  5. Xposed Module开发教程1

    看了一下文档,发现Xposed这货实在太强大了,它直接对Zygote做了一些手脚,简直是无所不能.缺点就是需要安装一下框架.基本原理就是把Android 系统的app_process给替换了,然后启动 ...

  6. 彻底解决phpcms v9升级后,文章发布出现: Mysql 1267错误:MySQL Error : Illegal mix of collations 解决办法

    彻底解决phpcms v9升级后,文章发布出现: MySQL Query : SELECT * FROM `withli_a`.`v9_keyword` WHERE `keyword` = '吼吼' ...

  7. 图标:适配不同分辨 的 hdpi、mdpi、ldpi 文件夹

    一:不同的layout Android手机屏幕大小不一,有480×320, 640×360, 800×480.怎样才能让App自动适应不同的屏幕呢? 其实很简单,只需要在res目录下创建不同的layo ...

  8. [转]云计算研究必备——精典Google论文

    Google云计算技术奠定其在业界的领先地位,收集经典云计算技术公开文章供大家研究学习: 01)GFS-The Google File System 02) Bigtable - A Distribu ...

  9. web 音频文件自动播放(兼容所有浏览器)

    function AudioPerform() { var ua = navigator.userAgent.toLowerCase(); var audiopath = "$!{Templ ...

  10. 修改apache 2.4.6的MPM模式

    编辑配置文件/etc/httpd/conf.modules.d/00-mpm.conf #Select the MPM module which should be used by uncomment ...