POJ 1050 To the Max(二维降一维、最大子段和)

题目链接:点击这里

在这里插入图片描述

#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<climits>

using namespace std;
typedef long long ll;
const int MOD = 10000007;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 110;
int a[maxn][maxn];
int temp[maxn]; 

int main()
{
    int n;
    scanf("%d", &n);
    
	for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            scanf("%d", &a[i][j]);
    
    int ans = INT_MIN;
    for(int i = 1 ; i < n; i++)				//起始行 
	{
        memset(temp, 0, sizeof(temp));
        for(int j = i; j <= n; j++)			//终止行 
		{
			//第i行到第j行降维压缩,利用最大子段和思想处理 
            int sum = 0, res = INT_MIN;
            for(int k = 1; k <= n; k++)		//遍历列 
			{
                temp[k] += a[j][k];
                sum += temp[k];
                if(sum<0)	sum = 0;
                res = max(res, sum);
            }
            ans = max(ans, res);
        }
    }
    
    printf("%d\n", ans);
    return 0;
}
发布了727 篇原创文章 · 获赞 111 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/qq_42815188/article/details/104264892