HDOJ 1081(ZOJ 1074) To The Max(动态规划)
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 <stdio.h>
#include <string.h>
#include <stdlib.h>
int a[2000];
int dp[150][150];
int main(){
int n;
while(scanf("%d",&n)==1){
int t;
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d",&t);
dp[i][j]=t+dp[i-1][j];
/// printf("i=%d",i);
}
}
// for(int i=0;i<=n;i++){
// for(int j=0;j<=n;j++){
// printf("%4d",dp[i][j]);
// }
// printf("\n");
// }
int maxx=-1000;
for(int i=1;i<=n;i++){
for(int j=i;j<=n;j++){
int sum=0;
for(int k=1;k<=n;k++){
t=dp[j][k]-dp[i-1][k];
sum+=t;
if(sum<0) sum=0;
if(sum>maxx) maxx=sum;
}
}
}
printf("%d\n",maxx);
}
return 0;
}
HDOJ 1081(ZOJ 1074) To The Max(动态规划)的更多相关文章
- ZOJ 1074 To the Max
原题链接 题目大意:这是一道好题.在<算法导论>这本书里面,有一节是介绍如何求最大子序列的.这道题有点类似,区别是从数组变成了矩阵,求最大子矩阵. 解法:完全没有算法功底的人当然不知道最大 ...
- ZOJ 1074 To the Max(DP 最大子矩阵和)
To the Max Time Limit: 2 Seconds Memory Limit: 65536 KB Problem Given a two-dimensional array o ...
- HDU 1074 Doing Homework (动态规划,位运算)
HDU 1074 Doing Homework (动态规划,位运算) Description Ignatius has just come back school from the 30th ACM/ ...
- 【动态规划】HDU 1081 & XMU 1031 To the Max
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1081 http://acm.xmu.edu.cn/JudgeOnline/problem.php?i ...
- HDU 1081 To The Max(动态规划)
题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rect ...
- POJ 1050 To the Max -- 动态规划
题目地址:http://poj.org/problem?id=1050 Description Given a two-dimensional array of positive and negati ...
- [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- ZOJ 2672 Fibonacci Subsequence(动态规划+hash)
题意:在给定的数组里,寻找一个最长的序列,满足ai-2+ai-1=ai.并输出这个序列. 很容易想到一个DP方程 dp[i][j]=max(dp[k][i])+1. (a[k]+a[i]==a[j], ...
- ZOJ 1074 最大子矩阵和
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
随机推荐
- [FTP] FTPHelper-FTP帮助类,常用操作方法 (转载)
点击下载 FTPHelper.zip 这个类是FTP服务器的一些操作1.连接FTP服务器 2.上传3.下载4.删除文件5.获取当前目录下明细(包含文件和文件夹) 6.获取FTP文件列表(包括文件夹) ...
- 在C#中实现Socket端口复用
转载:http://www.csharpwin.com/csharpspace/68.shtml 一.什么是端口复用: 因为在winsock的实现中,对于服务器的绑定是可以多重绑定的,在 ...
- js - 多个函数动态加载
//动态添加物流锁的IEMI列表. function createLi() { var r = '<s:property value="#session.locks"/> ...
- jsp - forward指令
forward指令 既可以指向静态的html页面,也可以转发到动态的jsp页面,并可以保留先前请求的参数. 例如,在web中新建一个Jsp_src.jsp的jsp页面: <%@ page lan ...
- KMP算法_读书笔记
下面是KMP算法的实现伪代码: KMP_MATCHER ( T, P ) . n = T.length . m = P.length . next = COMPUTE_PREFIX_FUNCTION ...
- Eclipse 经验之谈(一):快速打war包
如何快速打一个war包: 具体步骤: 单击右键[在项目名称上]——>Export -->War File . 完成war包的导出了.嘻嘻
- Action<>和Func<>区别
Action<>和Func<>其实都是委托的[代理]简写形式. 简单的委托写法: //普通的委托 public delegate void myDelegate(string ...
- c语言字符数组与字符串的使用详解
转自:http://www.jb51.net/article/37456.htm 1.字符数组的定义与初始化字符数组的初始化,最容易理解的方式就是逐个字符赋给数组中各元素.char str[10]={ ...
- View.setTag()的作用
//这个东西在一些需要用到Adapter自定控件显示方式的时候非常有用 //Adapter 有个getView方法,可以使用setTag把查找的view缓存起来方便多次重用 public View g ...
- 如何在本地安装测试ECSHOP 转载
如何在本地安装测试ECSHOP 如何在本地(自己的电脑)上先安装ECShop 一.创建PHP环境 1.下载AppServ 因为ECShop在线网上商店系统是用PHP语言开发的,所以,在本地架设网店之前 ...