1025:To the max(DP)
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.
4
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
15
#include<cstdio>
#include<cstring>
using namespace std;
const int N=105;
int a[N][N];
int b[N];
int main ()
{
int r,max=0;
scanf("%d",&r);
memset(a,0,sizeof(a));
for(int i=1;i<=r;i++)
{
for(int j=1;j<=r;j++)
{
scanf("%d",&a[i][j]);
a[i][j]+=a[i-1][j];
}
}
max=a[1][1];
for(int i=1;i<=r;i++)//从第i行开始到底j行。。。。转化成一维的
{
for(int j=i;j<=r;j++)
{
memset(b,0,sizeof(b));
for(int k=1;k<=r;k++)
{
if(b[k-1]>=0)
b[k]=b[k-1]+a[j][k]-a[i-1][k];
else
b[k]=a[j][k]-a[i-1][k];
if(max<b[k])
max=b[k];
}
}
}
printf("%d\n",max);
return 0;
}
#include<cstring>
using namespace std;
const int N=105;
int a[N][N];
int b[N];
int main ()
{
int r,max=0;
scanf("%d",&r);
memset(a,0,sizeof(a));
for(int i=1;i<=r;i++)
{
for(int j=1;j<=r;j++)
{
scanf("%d",&a[i][j]);
a[i][j]+=a[i-1][j];
}
}
max=a[1][1];
for(int i=1;i<=r;i++)//从第i行开始到底j行。。。。转化成一维的
{
for(int j=i;j<=r;j++)
{
memset(b,0,sizeof(b));
for(int k=1;k<=r;k++)
{
if(b[k-1]>=0)
b[k]=b[k-1]+a[j][k]-a[i-1][k];
else
b[k]=a[j][k]-a[i-1][k];
if(max<b[k])
max=b[k];
}
}
}
printf("%d\n",max);
return 0;
}
1025:To the max(DP)的更多相关文章
- 51Nod 1049:最大子段和(dp)
1049 最大子段和 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 N个整数组成的序列a[1],a[2],a[3],-,a[n],求该序列如a[i]+ ...
- 九度OJ 1153:括号匹配问题 (DP)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5193 解决:2248 题目描述: 在某个字符串(长度不超过100)中有左括号.右括号和大小写字母:规定(与常见的算数式子一样)任何一个左括 ...
- poj - 1050 - To the Max(dp)
题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 ...
- SCUT 125 :笔芯回文(DP)
https://scut.online/p/125 125. 笔芯回文 题目描述 bxbx有一个长度一个字符串SS,bxbx可以对其进行若干次操作. 每次操作可以删掉一个长度为k(1 \leq k \ ...
- POJ 3267:The Cow Lexicon(DP)
http://poj.org/problem?id=3267 The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submi ...
- HPU第三次积分赛-D:Longest Increasing Subsequence(DP)
Longest Increasing Subsequence 描述 给出一组长度为n的序列,a1,a2,a3,a4...an, 求出这个序列长度为k的严格递增子序列的个数 输入 第一行输入T ...
- 九度OJ 1077:最大序列和 (DP)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5600 解决:1637 题目描述: 给出一个整数序列S,其中有N个数,定义其中一个非空连续子序列T中所有数的和为T的"序列和&qu ...
- HDU 1231:最大连续子序列(DP)
pid=1231">最大连续子序列 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- HDU 1081 To The Max (dp)
题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rect ...
随机推荐
- 第三十四节,pickle数据类型转换二进制字节码模块
在使用pickle模块时需要先 import pickle 引入模块 pickle.dumps()模块函数 功能:将python各种类型的数据转换成计算机识别的二进制字节码[有参] 使用方法:pick ...
- tomcat 高并发配置 与优化
公司的一个服务器使用Tomcat6默认配置,在后台一阵全点击服务器就报废了,查了一下就要是PERMSIZE默认值过小造成(16-64) TOMCAT_HOME/bin/catalina.sh 添加一行 ...
- hdu_5806_NanoApe Loves Sequence Ⅱ(双指针)
题目链接:hdu_5806_NanoApe Loves Sequence Ⅱ 题意: 给你一段数,问你有多少个区间满足第K大的数不小于m 题解: 直接双指针加一下区间就行 #include<cs ...
- UIWebView是什么
UIWebView类是用来显示网络内容.要使用它,可以简单的创造一个UIWebView对象,放置到窗口上,并且发送一个指向网络内容的请求.通过这个类,可以控制网页历史的前进後退,也可以通过程序去控制网 ...
- Inno Setup入门(二十)——Inno Setup类参考(6)
存储框 存储框也是典型的窗口可视化组件,同编辑框类似,可以输入.显示文本,但是和编辑框不同的是,编辑框只能编辑.显示单行文本,而存储框则可以对多行文本进行操作.存储框的类定义如下:< xmlna ...
- docker !veth
https://github.com/docker/docker/issues/11889
- 为什么要在onNewIntent的时候要显示的去调用setIntent
一.原因: 当调用到onNewIntent(intent)的时候,需要在onNewIntent() 中使用setIntent(intent)赋值给Activity的Intent.否则,后续的getIn ...
- 如何修改android工程的包名?
在我们android项目开发到一定的程度时由于需要,我们必须修改一下工程的包名,以便更好的发布我们的项目.但是在这个过程中有时候修改好了之后会出现一些错误.下面由小编一步步教你如何更改包名,和解决出现 ...
- 软件设计模式详解:OCP原则
看到两篇关于OCP的文章, 纳之. 原文: http://www.cnblogs.com/muzongyan/archive/2010/08/05/1793454.html http://blog. ...
- DEAMONTOOLS: installation
installing daemontools .. adding -I /usr/include/errno.h to last first line of conf-cc mkdir -p /pac ...