2017北京网络赛 hihocoder 1580 Matrix (dp很好的思维题)

时间限制: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

大致题意:给你一个矩阵,你必须将其中的某个值改变成p,求最大子矩阵和

思路:膜一发dalao的题解http://blog.csdn.net/luricheng/article/details/78074046
我的理解:对于一般的求最大子矩阵的和,我们一般是枚举上下界O(n^2),然后再在O(n)的时间内求出最大连续子段和。然后对于这题,我们需要修改某个值变成p,那么在求最大连续子段这过程中我们可以加个二维dp,dp[i][0]表示此时以第i列结尾未改变值时的最大连续子段和,dp[i][1]表示此时以第i列结尾改变了值p的最大连续子段和。假设sum[i]为此时固定上下行后第i列所有值的和,minval[i]为固定上下行后第i列中最小的那个元素的值,那么转移方程即为
dp[i][0]=max(do[i-1][0],0)+sum[i];
dp[i][1]=max(dp[i-1][1]+sum[i],max(do[i-1][0],0)+sum[i]-minval[i]+p);
最后我们还需要考虑一下特殊情况,就是当我们所选的范围是整个矩形时,我们还需找到其中最小的值改成p

代码如下

#include 
using namespace std;  
const int N=305;
const int inf=-1e9-7;
const int INF=1e9+7;
int dp[N][2];
int mat[N][N];
int sum[N];
int minval[N];
int n,m,p;
int solve1()
{
    int ans=inf;
    dp[0][0]=sum[0];
    dp[0][1]=sum[0]-minval[0]+p;
    ans=max(dp[0][0],dp[0][1]);
    for(int i=1;i0]=max(dp[i-1][0],0)+sum[i];
        dp[i][1]=max(dp[i-1][1]+sum[i],max(dp[i-1][0],0)+sum[i]-minval[i]+p);
        ans=max(ans,max(dp[i][0],dp[i][1]));
    }
    return ans;
}
int solve2()//特判下,因为此时上下界为0到n-1,可能会选取整个矩形
{
    int ans=inf;
    for(int i=0;iint summ=0;
        int minn=INF;
        for(int j=i;jmin(minn,minval[j]);
            if(i==0&&j==m-1)
                ans=max(ans,summ-minn+p);
            else
                ans=max(ans,max(summ,summ-minn+p)); 
        }
    }
    return ans;
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&p)!=EOF)    
    {
        for(int i=0;ifor(int j=0;j"%d",&mat[i][j]);
        int ans=inf;

        for(int i=0;i//枚举行的上界
        {
            for(int f=0;f<=m;f++)//初始化
            {
                sum[f]=0;
                minval[f]=INF;
            }

            for(int j=i;j//枚举行下界
            {
                for(int k=0;kmin(minval[k],mat[j][k]);
                }
                if(i==0&&j==n-1)
                    ans=max(ans,solve2());
                else 
                    ans=max(ans,solve1());
            }
        }
        printf("%d\n",ans);
    }
    return 0; 
} 

你可能感兴趣的:(dp,思维题)