2017icpc北京网络赛c题(最大连续子段和)

时间限制: 1000ms
单点时限: 1000ms
内存限制: 256MB

描述

Once upon a time, there was a little dog YK. One day, he went to an antique shop and was impressed by a beautiful picture. YK loved it very much.

However, YK did not have money to buy it. He begged the shopkeeper whether he could have it without spending money.

Fortunately, the shopkeeper enjoyed puzzle game. So he drew a n × m matrix on the paper with integer value ai,j in each cell. He wanted to find 4 numbers x, y, x2, and y2(x ≤ x2, y ≤ y2), so that the sum of values in the sub-matrix from (x, y) to (x2, y2) would be the largest.

To make it more interesting, the shopkeeper ordered YK to change exactly one cell's value into P, then to solve the puzzle game. (That means, YK must change one cell's value into P.)

If YK could come up with the correct answer, the shopkeeper would give the picture to YK as a prize.

YK needed your help to find the maximum sum among all possible choices.

输入

There are multiple test cases.

The first line of each case contains three integers n, m and P. (1 ≤ n, m ≤ 300, -1000 ≤ P ≤ 1000).

Then next n lines, each line contains m integers, which means ai,j (-1000 ≤ ai,j ≤ 1000).

输出

For each test, you should output the maximum sum.

样例输入
3 3 4
-100 4 4
4 -10 4
4 4 4
3 3 -1
-2 -2 -2
-2 -2 -2
-2 -2 -2
样例输出
24
-1

解题思路:先考虑一种简单的一维情况,给你一个序列,你可以改变序列的一个数,让你求这个序列可以组成的最大连续子段和是多少。然后在考虑更简单的一种情况,没有修改操作,那么我们可以直接dp解决,回忆求解最大连续子段的思路,我们容易得到,我们可以在O(n)时间复杂内得到以位置i结尾的最大连续子段和,所以现在我们加上修改操作,那么我们可以枚举这个修改的位置,分两种情况,第一种,p 大于这个位置上的数,那么我们肯定要把这个数换成p,然后加上以i + 1位置结尾的最大连续字段和,再加上从后面某个位置到i + 1(反过来,以i + 1结尾,这个相当于后缀,我们像前面一样,从后往前做一次就行)的最大连续子段和,第二种,p 小于这个数,那就没有必要换了。这样的话一维我们就解决,回到原问题,就把一维问题扩展成了二维,这个简单,我们枚举矩阵的上下边界就行,然后把中间的很多数当成一个数就行,就变成了一维问题,总的时间复杂度为O(n * n * n),最后注意一点,当我们枚举上下边界为1和n时,注意当左右边界为1 , m也就是整个矩阵时,这个时候必须要换,其他情况,根据p和最小值的大小情况决定换不换。


#include <bits/stdc++.h>
using namespace std;
const int maxn = 300 + 10;
int inf = 0x3f3f3f3f;
int n, m, p;
int a[maxn][maxn];//原矩阵
int sum[maxn][maxn];//sum[i][l]表示第i列,第1行到第r行的和
int Min[maxn][maxn][maxn];//Min[i][l][r]表示第i列,第l行到第r行的最小值
int pre[maxn];//位置i前面的最大连续子段和
int suf[maxn];//位置i后面的最大连续子段和
int ans;
void init()
{
    ans = -inf;
    for(int i = 1; i <= m; i++)
    {
        sum[i][0] = 0;
        for(int j = 1; j <= n; j++)
        {
            sum[i][j] = sum[i][j - 1] + a[j][i];
        }
    }
    for(int i = 1; i <= m; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            int mm = a[j][i];
            for(int k = j; k <= n; k++)
            {
                if(a[k][i] < mm)
                {
                    mm = a[k][i];
                }
                Min[i][j][k] = mm;
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d%d", &n, &m, &p))
    {
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
                scanf("%d", &a[i][j]);

        init();
        //u = 1, d = n
        for(int i = 1; i <= m; i++)
        {
            int ssum = 0;
            int mmin = inf;
            for(int j = i; j <= m; j++)
            {
                ssum += sum[j][n];
                mmin = min(mmin, Min[j][1][n]);
                if(i == 1 && j == m)//必须要换
                {
                    ans = max(ans, ssum - mmin + p);
                }
                else
                {
                    if(p > mmin)
                    {
                        ans = max(ans, ssum - mmin + p);
                    }
                    else ans = max(ans, ssum);
                }
            }
        }
        for(int i = 1; i <= n; i++)
        {
            for(int j = i; j <= n; j++)
            {
                if(i == 1 && j == n) continue;//上面已经单独考虑过了
                int acc = 0;
                pre[0] = 0;
                suf[m + 1] = 0;
                for(int k = 1; k <= m; k++)
                {
                    int value = sum[k][j] - sum[k][i - 1];
                    pre[k] = acc + value;
                    if(acc + value > 0) acc += value;
                    else acc = 0;
                }
                acc = 0;
                for(int k = m; k >= 1; k--)
                {
                    int value = sum[k][j] - sum[k][i - 1];
                    suf[k] = acc + value;
                    if(acc + value > 0) acc += value;
                    else acc = 0;
                }
                //枚举要变得位置
                for(int k = 1; k <= m; k++)
                {
                    if(Min[k][i][j] < p)
                    {
                        int value = 0;
                        if(pre[k - 1] > 0) value += pre[k - 1];
                        if(suf[k + 1] > 0) value += suf[k + 1];
                        value += (sum[k][j] - sum[k][i - 1] - Min[k][i][j] + p);
                        ans = max(ans, value);
                    }
                    else
                    {
                        int value = 0;
                        if(pre[k - 1] > 0) value += pre[k - 1];
                        if(suf[k + 1] > 0) value += suf[k + 1];
                        value += (sum[k][j] - sum[k][i - 1]);
                        ans = max(ans, value);
                    }
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值