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 ...
随机推荐
- Google Developing for Android 学习总结
避免在循环中使用内存 也可理解为在循环中尽可能少创建对象,自定义控件避免在ondraw里面频繁创建paint对象. 尽可能避免内存分配 对象缓存: 常量通过类级别或者静态来进行缓存. 对象池: 同 ...
- 命令窗口修改编码,CMD编码修改方法
cmd中的编码方式为ANSI,若中文不是此编码方式则会出现乱码.作为程序员,会经常使用命令窗口查看执行日志,但是有时编码格式不对,大部分都是UTF8,在网上搜索了不少方法,很多没什么用,在这里教一个具 ...
- MFC中为菜单或按钮添加快捷键功能
1.新建一快捷键资源,ACCELERATOR,关联相应的ID号,下图所示中,其中,第一个ID为自定义快捷键ID,按CTRL+R,此时响应该ID以应的消息响应函数, 第二个ID为菜单ID,此时按CTRL ...
- Chapter 2 Open Book——16
By Friday I was perfectly comfortable entering my Biology class, nolonger worried that Edward would ...
- Ajax应用-Ajax传输JSON数据实例
———————————————————— <script type="text/javascript"> var client; ...
- hdu_5753_Permutation Bo(找规律)
题目连接:hdu_5753_Permutation Bo 题意: 给你一个有n个数的数列c1~cn,h1~hn为1~n的排列,求ci[hi>hi-1 and hi>hi+1]的期望和. ...
- configure PUTTY to not time out
To modify an existing session with "keep alives" to maintain your connection follow the st ...
- iOS使用NSMutableAttributedString
在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦 ...
- webstrom 快捷键(Idea可用)
在File-->setting可查看和配置功能快捷键,以下列出常用的快捷键 1. ctrl + shift + n: 打开工程中的文件,目的是打开当前工程下任意目录的文件. 2. ctrl + ...
- 移植WL18XX到高通的时候,会出现几个.KO文件没有编译出来的情况
1.检查kernel的.config文件,看是否有CONFIG又恢复了.这个时候就要需找依赖.把依赖使能 2.然后再去驱动源码检查 KCONFIG 的依赖,使能改使能的配置就可以了.