To The Max

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12471    Accepted Submission(s): 5985

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 <iostream>
#include <algorithm>
#include <vector>
#include<string.h>
using namespace std;
int a[][];
int main()
{
int n,m;
cin>>n>>m;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>a[i][j];
a[i][j]+=a[i-][j];
}
}
int dp[];
memset(dp,,sizeof(dp));
int ans=-0x3fffffff;
for(int i=;i<n-;i++)
{
for(int j=i+;j<n;j++)
{
for(int k=;k<=m;k++)
{
dp[k]=a[j][k-]-a[i][k-];
dp[k]=dp[k]+dp[k-]; if(dp[k]>ans) ans=dp[k];
if(dp[k]<) dp[k]=;
}
}
}
printf("%d\n",ans);
return ;
}

令a[i][k]保存第k列,前i行的和。这样将矩阵通过a[j][k]-a[i][k]压缩成一维,然后求最大字串和。。

最大子矩阵 hdu1081的更多相关文章

  1. hdu1081(最大子矩阵)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1081 分析:a[i][j]代表第i行,前j个数据的和:那么由a[i][j]可得sum[k][long] ...

  2. 【 HDU1081 】 To The Max (最大子矩阵和)

    题目链接 Problem - 1081 题意 Given a two-dimensional array of positive and negative integers, a sub-rectan ...

  3. hdu1081 最大子矩阵

    最大子矩阵自然直在最大连续子序列的升级版  只是其原理都是用到了动态规划思想     仅仅是矩阵用到了枚举 +合并       把非常多列看成是一列的和 #include<stdio.h> ...

  4. HDU1081:To The Max(最大子矩阵,线性DP)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1081 自己真够垃圾的,明明做过一维的这种题,但遇到二维的这种题目,竟然不会了,我也是服了(ps:猪啊). ...

  5. ACM 中 矩阵数据的预处理 && 求子矩阵元素和问题

            我们考虑一个$N\times M$的矩阵数据,若要对矩阵中的部分数据进行读取,比如求某个$a\times b$的子矩阵的元素和,通常我们可以想到$O(ab)$的遍历那个子矩阵,对它的各 ...

  6. [BZOJ1127][POI2008] KUP子矩阵

    Description 给一个n*n的地图,每个格子有一个价格,找一个矩形区域,使其价格总和位于[k,2k] Input 输入k n(n<2000)和一个n*n的地图 Output 输出矩形的左 ...

  7. 【SCOI2005】 最大子矩阵 BZOJ 1084

    Description 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分值之和最大.注意:选出的k个子矩阵不能相互重叠. Input 第一行为n,m,k(1≤n≤100,1≤m≤2 ...

  8. 一个N*M的矩阵,找出这个矩阵中所有元素的和不小于K的面积最小的子矩阵

    题目描述: 一个N*M的矩阵,找出这个矩阵中所有元素的和不小于K的面积最小的子矩阵(矩阵中元素个数为矩阵面积) 输入: 每个案例第一行三个正整数N,M<=100,表示矩阵大小,和一个整数K 接下 ...

  9. HDU1559 最大子矩阵 (二维树状数组)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others)  ...

随机推荐

  1. Java9模块化(Jigsaw)初识

    Java9经历了多次跳票,终于要在9月份正式发布,原计划Jigsaw在Java7就有的,也终于在Java9里面提供了,简单总结下. 对比 Java9 以前 上面2个图分别对应的分别是JDK8/9的目录 ...

  2. 第二章:ES索引说明

    1."_boost": 20评分的权重(排序) 2."analyzer": "ik"中文分词 "analyzer": & ...

  3. 【实践】js实现windows系统日历

    思路:1.定义好每一个月份的日期天数 2.获取当前的系统日期初始化数据 3.输出日历    2.1.先获取当前月的第一天是星期几(这一点与日历的排版至关重要!)    2.2.获取当前月的天数    ...

  4. 通过NAT实例实现外部访问AWS的ElastiCache资源

    ElastiCache作为AWS的内存缓存组建可以说做的一点也不友好了, 你可以通过ElastiCache创建redis,memcache的实例,却不能被外部访问. 背景 人傻钱多的客户总有人傻钱多的 ...

  5. Android编程心得-使用ActionBar+Fragment+ViewPager实现动态切换Menu效果

    1.首先上效果图 2.本例实现的效果主要适用于当前页面有多个页签时.进行Fragment切换时,能够利用不同的Menu样式与当前Fragment中的内容进行配合,能够大大添加复用性,看到效果图后,以下 ...

  6. v - bind

    1. 用于处理html标签的动态属性,即动态赋值(动态地绑定一个或多个特性,或一个组件 prop 到表达式) 2. 官网API <!DOCTYPE html> <html lang= ...

  7. 一步一步实现iOS微信自己主动抢红包(非越狱)

    前言:近期笔者在研究iOS逆向project,顺便拿微信来练手,在非越狱手机上实现了微信自己主动抢红包的功能. 题外话:此教程是一篇严肃的学术探讨类文章,只用于学习研究,也请读者不要用于商业或其它非法 ...

  8. tmux入门 : 3. 会话

      上一节我们已经将 tmux 安装好了,现在就可以通过以下命令来启动它: $ tmux 启动之后,可以看到命令行最底部多了一条绿色的状态条,上面显示了一些信息,比如计算机名和时间等. 要退出 tmu ...

  9. background-position 之剑走偏锋

    转自:http://www.cnblogs.com/yizuierguo/archive/2009/03/10/1407860.html 在设置background-image属性时,经常会遇到一个b ...

  10. Mac系统使用命令行快捷打开Sublime

    本篇文章由:http://xinpure.com/use-command-line-shortcuts-to-open-the-mac-system-sublime/ 方法一 使用软链接 ln -s ...