HDOJ-1081 To The Max

这道题是最大连续子序列之和的变形.
要求第i行到第j行的最大矩阵,就把i到j行元素按列相加,就是求最大连续子序列之和了.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;
#define INF 1e8
int visit[105][105];
int dp[105], sum[105], n;
int cal()
{
    int maxs = sum[1];
    dp[1] = sum[1];
    for(int i = 2; i <= n; i++)
    {
        dp[i] = max(dp[i-1] + sum[i], sum[i]);
        maxs = max(maxs, dp[i]);
    }
    return maxs;
}
int main()
{
   // freopen("in.txt", "r", stdin);
    while(cin >> n)
    {
        int  maxs = -INF;
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
        {
               cin >> visit[i][j];
               visit[i][j] += visit[i-1][j];
        }
        for(int i = 0; i < n; i++)
            for(int j = i+1; j <= n; j++)
        {
            for(int h = 1; h <= n; h++)
                sum[h] = visit[j][h] - visit[i][h];
            maxs = max(maxs, cal());
        }
        cout << maxs << endl;
    }
    return 0;
}

你可能感兴趣的:(HDOJ-1081 To The Max)