hihocoder 1643 Puzzle Game(北京icpc2017 现场赛)

#1634 : Puzzle Game

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

输入

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.

输出

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

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

题意:求子矩阵和的最大值最小,如果把一个数改为p使所求变大,可以不修改。

思路:当你要修改一个数的时候,整个矩阵的最大值可能是左边的一块,右边的一块,下面的和上面的,和当前块的最大值-a[i][j]+p;



我们直接暴力枚举所有的数,如果a[i][j]>p,进行操作

上下两块:ans=max(u[i-1],d[i+1]);

左右两块:ans=max(ans,max(l[j-1],r[j+1]));

ans1=min(ans1,max(u[n]-a[i][j]+p,ans));

为什么是u【n】呢,因为如果修改的不是在最大块里,

那么 max(u[n]-a[i][j]+p,ans)=u【n】,只有修改最大块才会变小。

#include<iostream>
#include<cmath>
#include<cstring> 
#include<cstdlib>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,p,sum[500],dp[500],a[500][500],u[500],d[500],r[500],l[500];
void solve1()
{   memset(dp,0,sizeof(dp));
	for(int i=1;i<=n;i++)
	  	   {
	  	   memset(sum,0,sizeof(sum));	
	  	   for(int j=i;j<=n;j++)
	  	      {
	  	      int t=-1000000000,tmp=t;
	  	      for(int k=1;k<=m;k++)
				  {
				  	sum[k]+=a[j][k];
				  	dp[k]=max(dp[k-1],0)+sum[k];
				  	tmp=max(dp[k],tmp);
				   } 
			  for(int k=j;k<=n;k++) u[k]=max(u[k],tmp);	
			  for(int k=1;k<=i;k++) d[k]=max(d[k],tmp);   
		      }
	       }
	//for(int i=1;i<=n;i++) printf("%d %d\n",u[i],d[i]);
}
void solve2()
{
	memset(dp,0,sizeof(dp));
	for(int i=1;i<=m;i++)
	  	   {
	  	   memset(sum,0,sizeof(sum));	
	  	   for(int j=i;j<=m;j++)
	  	      {
	  	      int t=-1000000000,tmp=t;
	  	      for(int k=1;k<=n;k++)
				  {
				  	sum[k]+=a[k][j];
				  	dp[k]=max(dp[k-1],0)+sum[k];
				  	tmp=max(dp[k],tmp);
				   } 
			  for(int k=j;k<=m;k++) l[k]=max(l[k],tmp);	
			  for(int k=1;k<=i;k++) r[k]=max(r[k],tmp);   
		      }
	       }
}
int main()
{   
	while(~scanf("%d%d%d",&n,&m,&p))
	  {
	  	for(int i=0;i<=400;i++) l[i]=r[i]=u[i]=d[i]=-100000000;
	  	for(int i=1;i<=n;i++)
	  	   for(int j=1;j<=m;j++) scanf("%d",&a[i][j]);
	    solve1();solve2();
	    int ans1=u[n]; //printf("%d\n",u[n]);
	    for(int i=1;i<=n;i++)
	       for(int j=1;j<=m;j++)
	          {
	          	if(a[i][j]<=p) continue;
	          	int ans=-1000000000;
	          	ans=max(u[i-1],d[i+1]);
	          	ans=max(ans,max(l[j-1],r[j+1]));
	          	ans1=min(ans1,max(u[n]-a[i][j]+p,ans));
			  }
		printf("%d\n",ans1);
	  }
}

posted @ 2018-05-01 14:39  The-Pines-of-Star  阅读(236)  评论(0编辑  收藏  举报