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. You must have a TTY to run sudo

    1.方法一,给命令行添加tty ssh -t user@foo.com 2.使用sudo visudo修改配置: Defaults:user !requiretty 表示用户user使用sudo命令时 ...

  2. [转]Understanding Integration Services Package Configurations

    本文转自:http://msdn.microsoft.com/en-us/library/cc895212.aspx Introduction With the 2008 release, SQL S ...

  3. metal的gpu query

    https://developer.apple.com/documentation/metal/mtlcommandbuffer/1639924-gpustarttime gpuStartTime 看 ...

  4. netmap -- ixgbe

    利用netmap抓ixgbe网卡上的以太网帧,跟e1000e网卡有区别. 使用e1000.e1000e网卡发以太网帧只要以太网帧的格式正确就可以了.只要格式和 目的MAC.源MAC地址正确,网卡就可以 ...

  5. Short与Integer互转

    int 是4字节, short 是2字节的, 如果将int(Integer)转成short(Short), 那么必须强制转换,否则会报编译异常. 但是, 当int(Integer)是一个final时, ...

  6. webDriver API——第11部分Remote WebDriver

    The WebDriver implementation. class selenium.webdriver.remote.webdriver.WebDriver(command_executor=' ...

  7. OpenERP(odoo)开发实例之搜索检索过去3个月的数据

    转自:http://www.chinamaker.net/ OpenERP(odoo)开发实例之搜索过滤:检索过去3个月的数据 解决这个问题的重点在于 relativedelta 的应用 示例代码如下 ...

  8. Odoo环境下Ubuntu服务器性能优化--参数调整

    公司在使用Odoo进行内部信息化管理,随着业务增长,服务器性能问题变成了瓶颈,为了解决这些问题,最近的工作重点将移到性能调整上来,同时也会在此记录整个处理过程,以便日后回顾. 1.根据相关资料建议,在 ...

  9. [Exception Android 22] - Could not find com.android.support:design:23.1.1

    Error:A problem occurred configuring project ':app'. > A problem occurred configuring project ':f ...

  10. c++11 学习

    #include <iostream> // std::cout #include <functional> // std::ref #include <thread&g ...