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 ...
随机推荐
- HDU 5543 Pick The Sticks
背包变形.与普通的背包问题不同的是:允许有两个物品可以花费减半. 因此加一维即可,dp[i][j][k]表示前i个物品,有j个花费减半了,总花费为k的情况下的最优解. #pragma comment( ...
- 在Activity之间传递数据—简单数据/Bundle
1.首先要知道怎么通过一个Activity 打开另一个Activity.主页面为MainActivity,另一个页面为OtherActivity.MainActivity中的按钮Id为btnStart ...
- hdu_4714_Tree2cycle(树形DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 题意:给你N个点N-1条边,形成一个树,让你拆树,并连接成一个环,每拆一次,连接一次,消耗1,问 ...
- 初识Iaas,paas
Iaas(Infrastructure-as-a-service),直译为基础设备作为一种服务. Paas(Platform as a service),直译为平台作为一种服务. 暂且忘掉这两个单词, ...
- Java操作PDF之itext入门
转载:http://lichunhui.iteye.com/blog/1550584 iText是著名的开放项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档 ...
- lvs + keepalived + httpd 高可用集群(转)
实验信息和拓扑:备注:Centos 6.5 selinux –disabled iptables off ServerName Ipaddress information LVSMaster 172. ...
- 免费 WebOffice使用
目前WebOffice使用比较多主要有两个公司的产品,分别是江西金格和北京点聚.但是点聚的是免费的,虽然有欠缺之处,但是经过个人修改还是比较好用的,关键一点是,它免费啊! 把一个最主要加载页面,如果读 ...
- xmlHTTP技术资料
一.数据库远程管理技术 基于互联网的广域网现代应用中的一个重要环节是数据库远程监控.首先简单回顾一下互联网上的数据库远程管理技术的发展过程和方式: 早期通过编写CGI-BIN程序模块进行数据库远程管理 ...
- linux学习的哲学层面的思考-架构
参考:http://blog.chinaunix.net/uid-26119273-id-3356414.html 学习Linux,准备做产品的话,不要把Linux当成了终极目标(当然,这是对应用而言 ...
- 转: oracle 存储过程 执行动态 实现sql
http://jingyan.baidu.com/article/5d6edee2fbb9f999eadeecb9.html http://jingyan.baidu.com/article/3638 ...