To The Max

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12471    Accepted Submission(s): 5985

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
 
#include <iostream>
#include <algorithm>
#include <vector>
#include<string.h>
using namespace std;
int a[][];
int main()
{
int n,m;
cin>>n>>m;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>a[i][j];
a[i][j]+=a[i-][j];
}
}
int dp[];
memset(dp,,sizeof(dp));
int ans=-0x3fffffff;
for(int i=;i<n-;i++)
{
for(int j=i+;j<n;j++)
{
for(int k=;k<=m;k++)
{
dp[k]=a[j][k-]-a[i][k-];
dp[k]=dp[k]+dp[k-]; if(dp[k]>ans) ans=dp[k];
if(dp[k]<) dp[k]=;
}
}
}
printf("%d\n",ans);
return ;
}

令a[i][k]保存第k列,前i行的和。这样将矩阵通过a[j][k]-a[i][k]压缩成一维,然后求最大字串和。。

最新文章

  1. nginx简易教程
  2. [moka同学笔记]php 获取时间(今天,昨天,三天内,本周,上周,本月,三年内,半年内,一年内,三年内)
  3. 17、文案人员 - IT软件人员书籍系列文章
  4. 转:C语言 可变参数
  5. Centos 7 安装LAMP环境
  6. RT-Thread信号量的基本操作
  7. 修改数据库mysql字符编码为UTF8
  8. 阿里云centos配置ftp和svn全过程
  9. idea新建maven项目时,mvn archetype:generate 速度缓慢
  10. MFC程序中消息以及函数的处理顺序简介[转]
  11. Node.js事件循环
  12. C#版-百度网盘API的实现(二)
  13. 自己动手实现一个简单的JSON解析器
  14. 较为复杂的实现Parcelable的子类--推荐Android中使用
  15. redis主从同步配置
  16. 说一说本人对linux系统学习的方法和经验
  17. iOS UI基础-16.0 UIButton
  18. MEAN 26
  19. apache的&lt;directory&gt; 语句以及属性的含义
  20. python3 应用 nose_parameterized 实现unittest 参数化

热门文章

  1. iOS:时间格式化(标准时间转为时间戳、时间戳转为标准时间、时间戳转为日期)
  2. iOS:iOS开发系列–打造自己的“美图秀秀”(上)
  3. 向PE文件植入后门代码技术讨论
  4. 流畅的python第一章python数据模型学习记录
  5. ORA-01219:数据库未打开:仅允许在固定表/视图中查询
  6. 加加减减(你真的懂++--吗) C#
  7. 记一次MySQL找回用户数据
  8. [Unity3D]Unity3D游戏开发之Lua与游戏的不解之缘终结篇:UniLua热更新全然解读
  9. 赵雅智_service生命周期
  10. 算法笔记_055:蓝桥杯练习 Tricky and Clever Password (Java)