HDU1081 To The Max 最大子矩阵和

题意:求最大子矩阵和。

解题思路:枚举上下边界 ,用一维思路去搞。

解题代码:

 1 // File Name: 1081.cpp
 2 // Author: darkdream
 3 // Created Time: 2015年04月01日 星期三 16时57分14秒
 4
 5 #include<vector>
 6 #include<list>
 7 #include<map>
 8 #include<set>
 9 #include<deque>
10 #include<stack>
11 #include<bitset>
12 #include<algorithm>
13 #include<functional>
14 #include<numeric>
15 #include<utility>
16 #include<sstream>
17 #include<iostream>
18 #include<iomanip>
19 #include<cstdio>
20 #include<cmath>
21 #include<cstdlib>
22 #include<cstring>
23 #include<ctime>
24 #define LL long long
25 #define maxn 1005
26 using namespace std;
27 int n ;
28 int mp[maxn][maxn];
29 int dp[maxn];
30 int mx = -1e9 ;
31 void  solve(int l , int r)
32 {
33    memset(dp,0,sizeof(dp));
34   for(int j = 1;j <= n;j ++)
35     for(int i = l ;i <= r ;i ++)
36     {
37         dp[j] += mp[i][j];
38     }
39   int sum = 0 ;
40   for(int i = 1;i <= n;i ++)
41   {
42      if(sum < 0 )
43      {
44        sum = 0 ;
45      }
46      sum += dp[i];
47      if(sum > 0 )
48          mx = max(sum,mx);
49   }
50 }
51 int main(){
52    while(scanf("%d",&n) != EOF)
53    {
54        mx = -1e9;
55      for(int i = 1;i <= n;i ++)
56          for(int j = 1;j <= n ;j ++)
57          {
58            scanf("%d",&mp[i][j]);
59            mx = max(mx,mp[i][j]);
60          }
61      for(int i = 1;i <= n;i ++)
62          for(int j = i ;j <= n; j ++)
63          {
64            solve(i,j);
65          }
66       printf("%d\n",mx);
67    }
68 return 0;
69 }

时间: 2024-10-10 05:15:54

HDU1081 To The Max 最大子矩阵和的相关文章

hdu-1081 To The Max (最大子矩阵和)

http://acm.hdu.edu.cn/showproblem.php?pid=1081 To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10874    Accepted Submission(s): 5204 Problem Description Given a two-dimensional array

HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)

Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. I

hdu1081 To the max(dp 矩阵压缩)

To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8947    Accepted Submission(s): 4323 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectang

hdu1081 To the Max

直接暴力枚举所有子矩形至少需要O(n^4)的复杂度,显然这不是一个合理的解决方法. 上述方案忽略了矩形之间的联系,进行了过多不必要的计算. 实际上如果固定矩形的左右边界,则底边在i行的矩形内数值之和与底边在i-1行的矩形的关系为 f[i] = s[i] + max(0, f[i - 1]), s[i]表示当前行对应的数值之和. 本题枚举是切入口,通过枚举把所有矩形分成左右边界固定的矩形族,而后再进行动态规划,可降低复杂度至O(n^3). acm.hdu.edu.cn/showproblem.ph

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

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1081 自己真够垃圾的,明明做过一维的这种题,但遇到二维的这种题目,竟然不会了,我也是服了(ps:猪啊). 最终还是看了题解. 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #define inf 0x3f3f3f3f using namesp

HDOJ To The Max 1081【动态规划-详解求最大子矩阵】

To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9879    Accepted Submission(s): 4762 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectan

【HDU 1081】To The Max(求子矩阵元素和)

题目应该很容易看懂,是为了求一个矩阵之内最大的一个子矩阵的和. 子矩阵的和表示的是该矩阵内所有元素的和. 方法引入: 首先当然十分容易的可以想到一维求子段的和. 假设数组为a[110]; int sum = 0, MAX = 0,n; for (int i = 0; i < n; i++) { if (sum < 0) sum = 0; sum += a[i]; if (sum>MAX) MAX = sum; } 其中a[i]表示的是你所要积的范围大小,因为是一维,所以我们要积的是每一个

hdu1081 最大子矩阵

最大子矩阵自然直在最大连续子序列的升级版  不过其原理都是用到了动态规划思想     只是矩阵用到了枚举 +合并       把很多列看成是一列的和 #include<stdio.h> #include<iostream> #include<string.h> using namespace std; #define INF -10000000 int n,num[110][110],mark[110]; int linemax() { int x=0,i,Max=IN

hdu 1081 &amp;amp; poj 1050 To The Max(最大和的子矩阵)

转载请注明出处:http://blog.csdn.net/u012860063 Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum