HihoCoder - 1634 Puzzle Game (最大子矩陣和)

Puzzle Game

 

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 Bob whether he could have it without spending money.

Fortunately, Bob enjoyed puzzle game. He promised to give YK a picture for free if YK can solve a puzzle game for him.

Bob drew a puzzle board, which was a n×m matrix consisting of n×m cells. And there was an integer in each cell. A sub-matrix was a matrix that was a continuous part of the puzzle board (The whole puzzle board could also be called a sub-matrix). The value of a sub-matrix meant the sum of integers in the cells of the sub-matrix. The sub-matrix which had the largest value was called “the largest sub-matrix”. Bob wanted to make the value of the largest sub-matrix as small as possible by selecting one cell on the board and changed the integer in it into P. But if making that kind of change would not do anything good, he didn’t have to change any cell.

In such a situation, YK needed to calculate the minimum value of the largest sub-matrix Bob could get.

Input

There are no more than 120 test cases, but at most 3 test cases in which n >= 50 or m >=50.

The first line of each case contains three integers, above mentioned n, m and P (1<=n,m<=150,-1000<=P<=1000).

Then n lines follow. Each line contains m integers x1,x2…xm(-1000<=xi <=1000, i = 1…m).

These n lines are the n×m integers in the n×m cells of the puzzle board.

Output

For each test case, you should output the minimum value of the largest sub-matrix Bob could get.

Sample Input

3 3 -10
-100 4 4
4 -10 4
4 4 1
3 3 -1
-2 -2 -2
-2 -2 -2
-2 -2 -2

Sample Output

8
-2

題目鏈接:https://vjudge.net/problem/HihoCoder-1634

題目大意:給一個n*m的矩陣,問把一個點的值修改爲p後,矩陣的最大子矩陣和最小

思路:預處理出每個點的上下左右四個方向的最大子矩陣,遍歷修改每一個點後的最大子矩陣,記錄其中的最小值

代碼:

#include<bits/stdc++.h>
using namespace std;
const int N=205;
int a[N][N],han[N][N],lie[N][N],q[N],u[N],d[N],l[N],r[N];
int main()
{
    int n,m,p;
    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]);
        for(int i=1;i<=n;i++) //第i行前綴和
            for(int j=1;j<=m;j++)
            han[i][j]=han[i][j-1]+a[i][j];
        for(int i=1;i<=m;i++)//第i列前綴和
            for(int j=1;j<=n;j++)
            lie[i][j]=lie[i][j-1]+a[j][i];
        for(int i=0;i<=n+1;i++) u[i]=d[i]=-2e9;
        for(int i=0;i<=m+1;i++) l[i]=r[i]=-2e9;

        for(int i=1;i<=m;i++)
            for(int j=i;j<=m;j++)
        {
            q[0]=0;
            for(int k=1;k<=n;k++)
                q[k]=max(q[k-1]+han[k][j]-han[k][i-1],han[k][j]-han[k][i-1]);
            for(int k=1;k<=n;k++) 
                u[k]=max(u[k],q[k]); 
        }
        for(int i=1;i<=m;i++)
            for(int j=i;j<=m;j++)
        {
            q[n+1]=0;
            for(int k=n;k>=1;k--)
                q[k]=max(q[k+1]+han[k][j]-han[k][i-1],han[k][j]-han[k][i-1]);
            for(int k=1;k<=n;k++)
                d[k]=max(d[k],q[k]);
        }
        for(int i=1;i<=n;i++)
            for(int j=i;j<=n;j++)
        {
            q[0]=0;
            for(int k=1;k<=m;k++)
                q[k]=max(q[k-1]+lie[k][j]-lie[k][i-1],lie[k][j]-lie[k][i-1]);
            for(int k=1;k<=m;k++)
                l[k]=max(l[k],q[k]);
        }
        for(int i=1;i<=n;i++)
            for(int j=i;j<=n;j++)
        {
            q[m+1]=0;
            for(int k=m;k>=1;k--)
                q[k]=max(q[k+1]+lie[k][j]-lie[k][i-1],lie[k][j]-lie[k][i-1]);
            for(int k=1;k<=m;k++)
                r[k]=max(r[k],q[k]);
        }

        for(int i=2;i<=n;i++) u[i]=max(u[i],u[i-1]); //前i行最大子矩陣和
        for(int i=n-1;i>=1;i--) d[i]=max(d[i],d[i+1]);
        for(int i=2;i<=m;i++) l[i]=max(l[i],l[i-1]);
        for(int i=m-1;i>=1;i--) r[i]=max(r[i],r[i+1]);

        int ans=u[n];
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
        {
            int x=u[i-1];
            x=max(x,d[i+1]);
            x=max(x,l[j-1]);
            x=max(x,r[j+1]);
            x=max(x,u[n]-a[i][j]+p); 
            ans=min(ans,x);
        }
        printf("%d\n",min(ans,u[n]));
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章