hihocoder 1580(最大子矩阵变形)

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


给你 nmk,让你把矩阵的一个数替换成 k,必须换一个且只换一个,那么就是最大子矩阵,然后先用rmq处理一下最小值,就可以,直接替换了最小值了,在这里还有再加一个值,就是以前已经替换过了的 sum1,sum没什么好说的,就是常规的最大子矩阵的套路,sum1可以因为可以随时替换变量,所以是一直更新的,sum1可以从是 max(sum+替换这次的结果,以前替换的结果+k,替换这次的结果),


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int zhi[310][310];
int n,m;
int lg[310];
int st[310][310][20];
inline int rmq(int id,int l,int r){  
    if(l>r) return 0;  
    int k=lg[r-l+1];  
    return min(st[id][l][k],st[id][r-(1<<k)+1][k]);  
}

void init(int k)
{
    for(int j = 1; (1<<j) <= n; j++){  
        for(int i = 1; i + (1<<j) - 1 <= n; i++){  
            st[k][i][j] = min(st[k][i][j - 1], st[k][i + (1<<(j-1))][j - 1]);  
        }  
    }  
}

int main()
{
    lg[0]=-1;  
    for(int i=1;i<=305;++i) lg[i]=lg[i>>1]+1;  
   	int f;
    while(~scanf("%d%d%d",&n,&m,&f))
    {
    	memset(zhi,0,sizeof(zhi));
    	for(int i=1;i<=m;i++)
    		memset(st[i],0x3f,sizeof(st[i]));
	    for(int i=1;i<=n;i++)
	        for(int j=1;j<=m;j++)
	        {
	            int a;
	            scanf("%d",&a);
	            zhi[i][j]=zhi[i-1][j]+a;
	            st[j][i][0]=a;
	        }
	    for(int i=1;i<=m;i++)
	    	init(i);
		int sum=0;
		int sum1=0;
		int maxi=-0x3f3f3f3f;
		int maxm=-0x3f3f3f3f;

		int x;
		for(int i=0;i<n;i++)
		{
			for(int j=i+1;j<=n;j++)
			{
			    sum=0;
			    sum1=0;
			    int cnt=0;
			    for(int r=1;r<=m;r++)
			    {
			        int k=zhi[j][r]-zhi[i][r];
			        int pre=sum;
			        x=rmq(r,i+1,j);
			        if(sum>0)
			        {
			            sum+=k;
			            cnt+=j-i;
			        }
			        else
			        {
			            sum=k;
			            cnt=j-i;
			        }
			        if(sum1>0)
			        	sum1+=k;
			        else {
			        	sum1=k-x+f;
			        }
			        sum1=max(sum1,pre+k-x+f);
			        if(sum1>maxm)
			        {
			            maxm=sum1;
			        }
			        if(cnt<n*m)
			        {
			        	if(sum>maxm)
			        		maxm=sum;
			        }
			    }
			}
		}
		printf("%d\n",maxm );
		}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值