ZOJ 1074 To the Max(DP 最大子矩阵和)
To the Max
Time Limit: 2 Seconds Memory Limit: 65536 KB
Problem
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.
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.
Input
4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1
8 0 -2
Output
15
代码如下:
# include<stdio.h>
# include<string.h>
# define N
int main(){
int a[N][N],b[N];
int n,i,j,k;
while(scanf("%d",&n)!=EOF)
{
for(i=;i<n;i++)
for(j=;j<n;j++)
scanf("%d",&a[i][j]);
int max= -;
for(i=;i<n;i++) //第i行到第j行的最大子矩阵和
{
memset(b,,sizeof(b));
for(j=i;j<n;j++)
{
int sum=;
for(k=;k<n;k++)
{
b[k] += a[j][k];
sum += b[k];
if(sum<) sum=b[k];
if(sum>max) max=sum;
}
}
}
printf("%d\n",max);
}
return ;
}
ZOJ 1074 To the Max(DP 最大子矩阵和)的更多相关文章
- ZOJ 1074 To the Max
		
原题链接 题目大意:这是一道好题.在<算法导论>这本书里面,有一节是介绍如何求最大子序列的.这道题有点类似,区别是从数组变成了矩阵,求最大子矩阵. 解法:完全没有算法功底的人当然不知道最大 ...
 - ZOJ 3703 Happy Programming Contest(DP)
		
题目链接 输出路径,搞了一个DFS出来,主要是这里,浪费了好长时间. #include <cstdio> #include <string> #include <cstr ...
 - ZOJ 3211 Dream City(DP)
		
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3374 题目大意:JAVAMAN 到梦幻城市旅游见到了黄金树,黄金树上 ...
 - ZOJ 2702 Unrhymable Rhymes(DP)
		
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1702 题目大意:给定有很多数字组成的诗,譬如 “AABB”, “AB ...
 - ZOJ 3471 Most Powerful(DP + 状态压缩)
		
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4257 题目大意:有 n(2<=n<=10) 个原子,每两 ...
 - ZOJ 2852 Deck of Cards DP
		
题意: 一一个21点游戏. 1. 有三个牌堆,分别为1X,2X,3X. 2. 纸牌A的值为1,纸牌2-9的值与牌面面相同,10(T).J.Q.K的值为10,而而joke(F)的值为 任意大大. 3. ...
 - ZOJ 3623 Battle Ships 简单DP
		
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3623 题意:给出N种可以建造的船和对方的塔生命值L,每种船给出建造时 ...
 - ZOJ 4027 Sequence Swapping(DP)题解
		
题意:一串括号,每个括号代表一个值,当有相邻括号组成()时,可以交换他们两个并得到他们值的乘积,问你最大能得到多少 思路:DP题,注定想得掉头发. 显然一个左括号( 的最远交换距离由他右边的左括号的最 ...
 - ZOJ 3211 Dream City(线性DP)
		
Dream City Time Limit: 1 Second Memory Limit: 32768 KB JAVAMAN is visiting Dream City and he se ...
 
随机推荐
- Folk博客程序——可以运行于node环境和asp环境的博客系统
			
Folk是一个轻量.便携,同时又很强大且可高度定制的内容发布平台.Folk的目标是:让您可以轻松地与世界分享您的点滴. Folk使用世界流行的javascript语言开发,因此可以同时运行于node和 ...
 - Python初始值表示为无穷大
			
之前只知道设置变量的初始值为0.今天在写网络路径分析的时候,为了找到离任意坐标距离最近的节点,初始设置最短距离为无穷大,然后不断的去替换,直到找到最近的节点. 刚开始设置是min_dis = 9999 ...
 - centos 系统程序包安装记录
			
-添加sudoer su - vi /etc/sudoers 在root ALL=(ALL) ALL 下添加: pete ALL=(ALL) ALL -安装拼音: sudo yum install & ...
 - openstack libtray
			
OpenStack packages¶ Distributions release OpenStack packages as part of the distribution or using ot ...
 - “互联网+”引发IT人才招工荒-新华网安徽频道
			
"互联网+"引发IT人才招工荒-新华网安徽频道 "互联网+"引发IT人才招工荒
 - Android4.0 以后不允许在主线程进行网络连接
			
Android4.0 以后不允许在主线程进行网络连接,否则会出现 android.os.NetworkOnMainThreadException.因此,必须另起一个线程进行网络连接方面的操作. pac ...
 - Yii2  自定义Gii生成代码模板
			
我们使用一个例子来介绍如何定制代码模板.假设我们想要定制由 model 生成器生成的代码. 我们首先创建一个名为 protected/gii/model/templates/customer 的目录. ...
 - UVa 872 - Ordering 输出全拓扑排序
			
本题要求输出所有拓扑排序的序列. 还好本题的数据量不是非常大.限制在26个大写英文字母,故此能够使用递归法输出. 这个递归输出所有解在Leetcode非常多这种题目的,不小心的话,还是非常难调试的. ...
 - 【web开发学习笔记】ibatis学习总结
			
ibatis学习总结 ibatis数据库配置文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCT ...
 - JAVA Metrics度量工具 - Metrics Core 翻译
			
Metrics核心 翻译自Metrics官方文档: http://metrics.codahale.com/manual/core/ JAVA Metrics是一个用于度量的一个JAVA的类库,使用请 ...