hiho 1580 Matrix(dp 降维压缩)

来源:互联网 发布:js 修改style属性 编辑:程序博客网 时间:2024/06/13 06:27

#1580 : Matrix

时间限制: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 44 -10 44 4 43 3 -1-2 -2 -2-2 -2 -2-2 -2 -2
样例输出
24-1

这题最大的坑点就是 必须修改一个数但是这个数却不一定出现在所选的矩阵中

(需要特别考虑得情况 当所选的矩阵是整个矩阵时 应该正逆各枚举一遍序列)


#include <iostream>#include <stdio.h>#include <string.h>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <math.h>#include <bitset>#include <algorithm>#include <climits>using namespace std;const int N = 400;typedef long long LL;int n, m;LL p, sum[N][2], dp[N][4], a[N][N];// 0从1开始 1不从1开始 3最大的不选值LL get(int x, int y){    int flag=0;    LL res0=-1000000000,res1=-1000000000,ans=-1000000000;    for(int i=1;i<=m;i++)    {        if(i==1)        {            dp[i][0]=sum[i][0];            dp[i][1]=sum[i][1];        }        else        {            if(dp[i-1][0]+sum[i][0]>sum[i][0]) dp[i][0]=dp[i-1][0]+sum[i][0];            else flag=1,dp[i][0]=sum[i][0];            if(dp[i-1][0]+sum[i][1]>sum[i][1]) dp[i][1]=dp[i-1][0]+sum[i][1];            else dp[i][1]=sum[i][1];            if(dp[i-1][1]+sum[i][0]>dp[i][1]) dp[i][1]=dp[i-1][1]+sum[i][0];        }        res0=max(res0,dp[i][0]);        res1=max(res1,dp[i][1]);    }    if(x!=1||y!=n)    {        ans=max(ans,res0);        ans=max(ans,res1);    }    else    {        ans=max(ans,res1);        if(res0!=dp[m][0]) ans=max(ans,res0);        else if(flag) ans=max(ans,res0);        else        {            res0=-10000000000;            LL h=0;            for(int i=1;i<m;i++) ans=max(ans,dp[i][0]);            for(int i=m;i>1;i--)            {                if(i!=m)                {                    if(h+sum[i][0]>sum[i][0]) h=h+sum[i][0];                    else h=sum[i][0];                }                else h=sum[i][0];                ans=max(ans,h);            }        }    }    return  ans;}int main(){    while(scanf("%d %d %lld", &n, &m, &p)!=EOF)    {        for(int i=1; i<=n; i++)        {            for(int j=1; j<=m; j++)            {                scanf("%lld", &a[i][j]);            }        }        if(n==1&&m==1)        {            printf("%lld\n",p);            continue;        }        LL flag=-1, ans, cnt;        for(int i=1; i<=n; i++)        {            for(int j=1; j<=m; j++) sum[j][0]=a[i][j],sum[j][1]=p;            cnt=get(i,i);            if(flag==-1||cnt>ans) ans=cnt;            flag=1;            for(int k=i+1; k<=n; k++)            {                for(int j=1; j<=m; j++)                {                    sum[j][1]=max(sum[j][0]+p,sum[j][1]+a[k][j]);                    sum[j][0]+=a[k][j];                }                cnt=get(i,k);                if(cnt>ans) ans=cnt;            }        }        printf("%lld\n",ans);    }    return 0;}








原创粉丝点击