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. [系统开发] 基于Ansible的产品上线系统

    前言: 应部门急需,开发了一套基于Ansible Playbook的产品上线系统.由于时间很紧,UI直接套用了之前开发的一套perl cgi模板,后续计划用 django 重新编写. 个人感觉该系统的 ...

  2. 条款19:设计class犹如设计type

    1,新的type对象应该如何被创建和销毁? 这会影响class的构造函数ctor和析构函数dtor,以及内存分配函数和释放函数   //operator new //operator new[] // ...

  3. 【学】jQuery的源码思路2——$符号是如何封装的

    jQuery中的$符号功能很强大,原因在于对函数参数的个数以及种类的控制,还有对于面向对象思想的运用 function jQuery(args){ //接受参数,并对其判断 this.elements ...

  4. CSS详解

    Web前端开发css基础样式总结 颜色和单位的使用 颜色 用颜色的名字表示颜色,比如:red 用16进制表示演示 比如:#FF0000 用rgb数值表示颜色,rgb(红,绿,蓝),每个值都在0-255 ...

  5. log4net 配置

    1.是直接在代码中通过调用XmlConfigurator.Configure()来解析配置文件,配置日志环境. log4net.Config.XmlConfigurator.Configure(); ...

  6. stanford NLP学习笔记3:最小编辑距离(Minimum Edit Distance)

    I. 最小编辑距离的定义 最小编辑距离旨在定义两个字符串之间的相似度(word similarity).定义相似度可以用于拼写纠错,计算生物学上的序列比对,机器翻译,信息提取,语音识别等. 编辑距离就 ...

  7. mysql的故事

    所有的条件都分开理解,命令之间没有包含吗?

  8. win7、linux安装使用pip、mitmproxy

    安装pip https://pip.pypa.io/en/latest/installing.html 步骤: 下载 https://bootstrap.pypa.io/get-pip.py pyth ...

  9. C#实现堆栈

    堆栈(Stack)是一种特殊的线性表,是一种操作只允许在尾端进行插入或删除等操作的线性表.表尾允许进行插入删除操作,称为栈顶(Top),另一端是固定的,称为栈底(Bottom).栈的操作使按照先进后出 ...

  10. Google Developing for Android 一 - 相关上下文介绍

    前几天在G+上看到Google Developers站点,有一个Android系列的文章,分享到个人微博,周末闲来没事就学写了下,把它们简单的翻译了下,没想到一发不可收拾,六篇文章全部都翻译完了,有些 ...