1051 最大子矩阵和

1051 最大子矩阵和

基准时间限制:2 秒 空间限制:131072 KB 分值: 40 难度:4级算法题

一个M*N的矩阵,找到此矩阵的一个子矩阵,并且这个子矩阵的元素的和是最大的,输出这个最大的值。

例如:3*3的矩阵:

-1 3 -1

2 -1 3

-3 1 2

和最大的子矩阵是:

3 -1

-1 3

1 2

Input

第1行:M和N,中间用空格隔开(2 <= M,N <= 500)。 第2 - N + 1行:矩阵中的元素,每行M个数,中间用空格隔开。(-10^9 <= M[i] <= 10^9)

Output

输出和的最大值。如果所有数都是负数,就输出0。

Input示例

3 3

-1 3 -1

2 -1 3

-3 1 2

Output示例

7

//前缀和,轻松解决n*n

 1 # include <cstdio>
 2 # include <cstring>
 3 # include <cstdlib>
 4 # include <iostream>
 5 # include <vector>
 6 # include <queue>
 7 # include <stack>
 8 # include <map>
 9 # include <bitset>
10 # include <sstream>
11 # include <set>
12 # include <cmath>
13 # include <algorithm>
14 # pragma  comment(linker,"/STACK:102400000,102400000")
15 using namespace std;
16 # define LL          long long
17 # define pr          pair
18 # define mkp         make_pair
19 # define lowbit(x)   ((x)&(-x))
20 # define PI          acos(-1.0)
21 # define INF         0x3f3f3f3f3f3f3f3f
22 # define eps         1e-8
23 # define MOD         1000000007
24
25 inline int scan() {
26     int x=0,f=1; char ch=getchar();
27     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘) f=-1; ch=getchar();}
28     while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘; ch=getchar();}
29     return x*f;
30 }
31 inline void Out(int a) {
32     if(a<0) {putchar(‘-‘); a=-a;}
33     if(a>=10) Out(a/10);
34     putchar(a%10+‘0‘);
35 }
36 # define N 505
37 /**************************/
38 LL sum[N][N];
39
40 int main()
41 {
42     int n ,m;
43     scanf("%d%d",&m,&n);
44     for (int i=1;i<=n;i++)
45     {
46         for (int j=1;j<=m;j++)
47         {
48             int x = scan();
49             sum[i][j]=sum[i][j-1]+x;
50         }
51     }
52     LL ans=0;
53     for (int i=1;i<=m;i++)
54         for (int j=i;j<=m;j++)
55         {
56             LL tp=0;
57             for (int k=1;k<=n;k++)
58             {
59                 tp = max(sum[k][j]-sum[k][i-1]+tp,0LL);
60                 ans = max(tp,ans);
61             }
62         }
63     printf("%lld\n",ans);
64     return 0;
65 }

时间: 2024-08-01 22:28:21

1051 最大子矩阵和的相关文章

51Nod - 1051 最大子矩阵和

51Nod - 1051 最大子矩阵和 一个M*N的矩阵,找到此矩阵的一个子矩阵,并且这个子矩阵的元素的和是最大的,输出这个最大的值. 例如:3*3的矩阵: -1 3 -1 2 -1 3 -3 1 2 和最大的子矩阵是: 3 -1 -1 3 1 2 Input 第1行:M和N,中间用空格隔开(2 <= M,N <= 500). 第2 - N + 1行:矩阵中的元素,每行M个数,中间用空格隔开.(-10^9 <= M[i] <= 10^9) Output 输出和的最大值.如果所有数都

51nod 1051 最大子矩阵和(dp)

题目链接:51nod 1051 最大子矩阵和 实质是把最大子段和扩展到二维.读题注意m,n... 1 #include<cstdio> 2 #include<cstring> 3 #include<vector> 4 #include<algorithm> 5 #define CLR(a,b) memset((a),(b),sizeof((a))) 6 using namespace std; 7 const int N = 501; 8 int dp[N]

51 nod 1051 最大子矩阵和

1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 int dp[600][600]; 5 long long a[600]; 6 7 int main() 8 { 9 int n,m; 10 cin>>m>>n; 11 for(int i=0;i<n;i++){ 12 for(int j=0;j<m;j++){ 13 cin>>dp[i][j]; 1

51nod 1051 求最大子矩阵和

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1051 1051 最大子矩阵和 基准时间限制:2 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 一个M*N的矩阵,找到此矩阵的一个子矩阵,并且这个子矩阵的元素的和是最大的,输出这个最大的值. 例如:3*3的矩阵: -1 3 -1 2 -1 3 -3 1 2 和最大的子矩阵是: 3 -1 -1 3 1 2 Input 第1行:M和N,中间用空格

51nod四级题(第一波汇总)

1051 最大子矩阵和() 思路: 用前缀和维护成块 然后 和 最大子序列和 同理.前缀和这块 O(n2)  后面最大子序列和 O(n),O(n3). 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 500 + 5; 5 6 LL M[maxn][maxn], sum[maxn][maxn];// 第i行前j列的和 7 8 int main() 9 { 10

COGS 1051. [Vijos1021] Victoria的舞会1

1051. [Vijos1021] Victoria的舞会1 #include<bits/stdc++.h> using namespace std; #define maxn 2333 int n,m,x,num[maxn<<3],ans; char ch; inline void read(int &now) { ch=getchar(); now=0; while(ch>'9'||ch<'0') ch=getchar(); while(ch>='0'

BZOJ 1084 最大子矩阵 终于过了

一开始看到这道题,由于觉得m <= 2, 所以觉得这是道水题,回去后想了一下.在晚上来机房的时候已经想出来了,但是我必须承认细节决定成败.远在一个小时前我就已经把算法的主体都写好了,但是就是一直WA,为什么就是各种粗心,真心想捏死自己.一个小时就这么白白浪费了.我希望明天的我能变得强大一点.在有了今日惨痛的教训之后. 这道题并不难.用d[i][j][k] 来表示状态.i表示第几行,j表示之前取了多少个矩阵,k表示上一行的状态.即上一行的矩阵取法.如果k == 0 那么没有一个矩阵延伸到上一行,如

poj1050查找最大子矩阵和

题目: To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 48507   Accepted: 25662 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 locate

Codevs 1159 最大全0子矩阵 悬线法!!!!

1159 最大全0子矩阵 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个0,1方阵中找出其中最大的全0子矩阵,所谓最大是指O的个数最多. 输入描述 Input Description 输入文件第一行为整数N,其中1<=N<=2000,为方阵的大小,紧接着N行每行均有N个0或1,相邻两数间严格用一个空格隔开. 输出描述 Output Description 输出文件仅一行包含一个整数表示要求的最大的全零子矩阵中零的个数.