To The Max

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 11685    Accepted Submission(s): 5649

Problem Description
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 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 x 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
这道题的意思是给一个矩阵,求助最大子矩阵和,很容易联想到一维的最大字段和,所以我们可以把二维的转化为一维的
用上一个辅助数组 s[i][j]表示从第一行到底i行,第j列的所有元素的和,那个第i行到第k行第j列的和就为s[k][j]-s[i-1][j];
用for循环遍历即可转化为一维的情况
下面是ac代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
#define inf 0x3f3f3f3f int a[105][105];
int n;
int s[105][105];//j列1行i行的和
int b[105][105];//第i行到j行暂时保存这些行的情况 int main()
{
while(~scanf("%d",&n))
{
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
scanf("%d",&a[i][j]);
memset(s,0,sizeof(s));
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
s[i][j]=s[i-1][j]+a[i][j];
int mx=-1280000;
memset (b,0,sizeof(b));
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
for(int k=j; k<=n; k++)
{
if(b[j][k]<0)
b[j][k]=s[k][i]-s[j-1][i];
else
b[j][k]+=s[k][i]-s[j-1][i];
if(mx<b[j][k])
mx=b[j][k];
}
}
printf("%d\n",mx); } return 0;
}






hdu1081 To The Max 2016-09-11 10:06 29人阅读 评论(0) 收藏的更多相关文章

  1. Hadoop1.2.1伪分布模式安装指南 分类: A1_HADOOP 2014-08-17 10:52 1346人阅读 评论(0) 收藏

    一.前置条件 1.操作系统准备 (1)Linux可以用作开发平台及产品平台. (2)win32只可用作开发平台,且需要cygwin的支持. 2.安装jdk 1.6或以上 3.安装ssh,并配置免密码登 ...

  2. DateTime日期格式获取 分类: C# 2014-04-15 10:36 233人阅读 评论(0) 收藏

    c#.net 获取时间年月日时分秒格式 //获取日期+时间 DateTime.Now.ToString();            // 2008-9-4 20:02:10 DateTime.Now. ...

  3. 山东理工大学第七届ACM校赛-经济节约 分类: 比赛 2015-06-26 10:34 19人阅读 评论(0) 收藏

    经济节约 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 由于经济紧张,某国国王决定减少一部分多余的士兵,这些士兵在边界都有各自的 ...

  4. C语言基础总结 分类: iOS学习 c语言基础 2015-06-11 10:08 23人阅读 评论(0) 收藏

    //欲练此功必先自宫!!!     //第一天:C语言的基础     //进制     //2进制, 10进制, 8进制, 16进制     //注:8进制数前加0, 16进制数前加0x        ...

  5. 欧拉回路-Door Man 分类: 图论 POJ 2015-08-06 10:07 4人阅读 评论(0) 收藏

    Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2476 Accepted: 1001 Description ...

  6. 多校3- RGCDQ 分类: 比赛 HDU 2015-07-31 10:50 2人阅读 评论(0) 收藏

    RGCDQ Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practic ...

  7. 山东理工大学第七届ACM校赛-LCM的个数 分类: 比赛 2015-06-26 10:37 18人阅读 评论(0) 收藏

    LCM的个数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 对于我们来说求两个数的LCM(最小公倍数)是很容易的事,现在我遇到了 ...

  8. 山东理工大学第七届ACM校赛-完美素数 分类: 比赛 2015-06-26 10:36 15人阅读 评论(0) 收藏

    完美素数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 我们定义:如果一个数为素数,且这个数中含有7或3,那么我们称这个数为完美 ...

  9. 山东理工大学第七届ACM校赛-飞花的鱼塘 分类: 比赛 2015-06-26 10:30 43人阅读 评论(0) 收藏

    飞花的鱼塘 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 一日,飞花壕在稷下湖游玩,忽然,飞花巨有了一个养鱼的想法,于是,他大手 ...

随机推荐

  1. .Spark Streaming(上)--实时流计算Spark Streaming原理介

    Spark入门实战系列--7.Spark Streaming(上)--实时流计算Spark Streaming原理介绍 http://www.cnblogs.com/shishanyuan/p/474 ...

  2. 大型运输行业实战_day04_3_高级查询+分页

    1.高级查询+分页最终结果 2.分页的本质分析 前端传入:当前页  和  每页显示条数 数据库必须查询出:数据列表 和 总共条数 页面显示包括的数据有: 列表 +  每页显示条数 + 当前页 + 总共 ...

  3. Web标准:三、二列和三列布局

    知识点: 1.二列自适应宽度 2.二列固定宽度 3.二列固定宽度居中 4.xhtml的块级元素(div)和内联元素(span) 5.float属性 6.三列自适应宽度 7.三列固定宽度 8.三列固定宽 ...

  4. 基于MSAA的QQ界面信息获取的实现

    主要技术(Microsoft Active Accessibility)讲解: 以下是微软对于此技术的说明 Microsoft Active Accessibility Version 2.0 Pur ...

  5. Python bool() 函数

    Python bool() 函数  Python 内置函数 描述 bool() 函数用于将给定参数转换为布尔类型,如果没有参数,返回 False. bool 是 int 的子类. 语法 以下是 boo ...

  6. np.eye()

    今天在完成深度学习的相关编程作业的时候,发现代码中出现了一个关于np.eye()的函数,这个函数的用法非常的简单,但是在预制的代码中,这个函数的用法并非单单制造一个对角矩阵,而是通过其来将一个labe ...

  7. Windows系统之hosts文件

    对于Hosts文件相信很多Win7的系统用户会比较陌生,其实Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,所以功 ...

  8. python之多线程队列

    # 一共有以下3种队列# 1.先进先出# 2.后进先出# 3.存储数据的时候可设置优先级的队列,设置不同的优先级,取的时候按照优先级的顺序来取 下面介绍一下队列的方法,如果要使用队列,则需要导入一个模 ...

  9. (三)介绍简单的ROS命令

    1.ROS文件系统中的基本概念 Packages: Package是ROS系统中最底层最基本的组织,里面存放各种文件:库.工具.可执行文件等.Manifest: 一个package描述xml文件,定义 ...

  10. 【深度好文】多线程之WaitHandle-->派生-》Semaphore信号量构造

    Semaphore 继承自WaitHandle. 信号量说简单点就是为了线程同步,或者说是为了限制线程能运行的数量. //创建一个限制资源类 //资源数为5,开放资源数为2 //主线程自动占有3个资源 ...