hihocoder1634-Puzzle Game

#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,可以将矩阵中一个数字改为P,或者完全不修改,问最大子矩阵最小是多少

解题思路:先处理出最大子矩阵的位置和大小ma,然后枚举最大子矩阵中的位置,若a[i][j]<=P,那么不需要修改,若a[i][j]>P,那么当前矩阵的最大子矩阵和应该等于 max(上方最大子矩阵和,下方最大子矩阵和,左方最大子矩阵和,右方最大子矩阵和,ma-a[i][j]+P )


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int n, m, p;
int a[200][200];
int x2, y2, x3, y3, ma;
int f[200], mar1[200], mar2[200], mac1[200], mac2[200];

int main()
{
	while (~scanf("%d%d%d", &n, &m, &p))
	{
		mar1[0] = mar2[n + 1] = mac1[0] = mac2[m + 1] = -INF;
		for (int i = 1; i <= n; i++)
		{
			mar1[i] = mar2[i] = -INF;
			for (int j = 1; j <= m; j++)
			{
				mac1[j] = mac2[j] = -INF;
				scanf("%d", &a[i][j]);
			}
		}
		int ma = -INF;
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++) f[j] = 0;
			for (int j = i; j <= n; j++)
			{
				for (int k = 1; k <= m; k++) f[k] += a[j][k];
				int sum = 0, s = 1;
				mar1[j] = max(mar1[j - 1], mar1[j]);
				for (int k = 1; k <= m; k++)
				{
					if (sum <= 0) sum = f[k], s = k;
					else sum += f[k];
					mar1[j] = max(sum, mar1[j]);
					if (ma < sum)
					{
						ma = sum;
						x2 = i, x3 = j, y2 = s, y3 = k;
					}
				}
			}
		}
		for (int i = n; i >= 1; i--)
		{
			for (int j = 1; j <= m; j++) f[j] = 0;
			for (int j = i; j >= 1; j--)
			{
				for (int k = 1; k <= m; k++) f[k] += a[j][k];
				int sum = 0;
				mar2[j] = max(mar2[j + 1], mar2[j]);
				for (int k = 1; k <= m; k++)
				{
					if (sum <= 0) sum = f[k];
					else sum += f[k];
					mar2[j] = max(sum, mar2[j]);
				}
			}
		}
		for (int i = 1; i <= m; i++)
		{
			for (int j = 1; j <= n; j++) f[j] = 0;
			for (int j = i; j <= m; j++)
			{
				for (int k = 1; k <= n; k++) f[k] += a[k][j];
				int sum = 0;
				mac1[j] = max(mac1[j - 1], mac1[j]);
				for (int k = 1; k <= n; k++)
				{
					if (sum <= 0) sum = f[k];
					else sum += f[k];
					mac1[j] = max(sum, mac1[j]);
				}
			}
		}
		for (int i = m; i >= 1; i--)
		{
			for (int j = 1; j <= n; j++) f[j] = 0;
			for (int j = i; j >= 1; j--)
			{
				for (int k = 1; k <= n; k++)
					f[k] += a[k][j];
				int sum = 0;
				mac2[j] = max(mac2[j + 1], mac2[j]);
				for (int k = 1; k <= n; k++)
				{
					if (sum <= 0) sum = f[k];
					else sum += f[k];
					mac2[j] = max(sum, mac2[j]);
				}
			}
		}
		int temp = ma;
		for (int i = x2; i <= x3; i++)
		{
			for (int j = y2; j <= y3; j++)
			{
				if (a[i][j] <= p) continue;
				int mx = temp - a[i][j] + p;
				if (i > 1) mx = max(mx, mar1[i - 1]);
				if (i < n) mx = max(mx, mar2[i + 1]);
				if (j > 1) mx = max(mx, mac1[j - 1]);
				if (j < m) mx = max(mx, mac2[j + 1]);
				ma = min(ma, mx);
			}
		}
		printf("%d\n", ma);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值