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 ...
随机推荐
- myeclipse10 中修改html,servlet,jsp等的生成模板
1.进入myeclipse的安装目录 2.用减压软件,(如winrar)打开common\plugins\com.genuitec.eclipse.wizards_9.0.0.me2011080913 ...
- CSDN Oracle版精华帖荟萃
⑴ 关于大数据量的数据库设计问题http://bbs.csdn.net/topics/390382930⑵ ORA-00904标识符无效http://bbs.csdn.net/topics/39033 ...
- Html table 实现Excel多格粘贴
Html table 实现Excel多格粘贴 电商网站的后台总少不了各种繁杂数据的录入,旁边的运营妹子录完第138条商品的时候,终于忍不住转身吼到:为什么后台的录入表不能像Excel那样多行粘贴!!! ...
- Nodejs异步流程控制Async
http://www.cnblogs.com/huair_12/p/4117351.html 很好的总结 关联下 以便以后学习使用
- 输出第N个素数
输出第N个素数 public class FindNthPrime { public static void main(String[] args){ int N = Integer.parseInt ...
- Linux + C + Epoll实现高并发服务器(线程池 + 数据库连接池)(转)
转自:http://blog.csdn.net/wuyuxing24/article/details/48758927 一, 背景 先说下我要实现的功能,server端一直在linux平台下面跑,当客 ...
- tabBar选中底部弹出窗口
//UITabBarControllerDelegate方法 - (BOOL)tabBarController:(UITabBarController *)tabBarController shoul ...
- 浅析tornado web框架
tornado简介 1.tornado概述 Tornado就是我们在 FriendFeed 的 Web 服务器及其常用工具的开源版本.Tornado 和现在的主流 Web 服务器框架(包括大多数 Py ...
- 我学C的那些年[ch02]:宏,结构体,typedef
c语言的编译过程: 预处理 编译 汇编 链接 而预处理中有三种情况: 文件包含( #include ) 条件编译(#if,#ifndef,#endif) 宏定义( #define ) 宏就是预处理中的 ...
- 不同优化选项对ARM下C语言编译的影响
我们知道在C语言编译时,有那么几个常用的优化编译选项,分别是-O0,-O1,-O2,-O3以及-Os.之前一直觉得既然是优化选项,顶多是优化一下逻辑,提高一些效率或者减少一下程序大小而已.很少会觉得它 ...