博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hihocoder 1643 Puzzle Game(北京icpc2017 现场赛)
阅读量:4323 次
发布时间:2019-06-06

本文共 3493 字,大约阅读时间需要 11 分钟。

#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 44 -10 44 4 13 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
#include
#include
#include
#include
#include
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); }}

转载于:https://www.cnblogs.com/The-Pines-of-Star/p/9878833.html

你可能感兴趣的文章
再更新ww的mingw MinGW-full-20101119
查看>>
Benefit UVA - 11889
查看>>
全排列 最详细的解题报告
查看>>
c++ web服务器
查看>>
android机型排行榜(201509)
查看>>
eclipse + maven + scala+spark环境搭建
查看>>
jmeter中webdriver插件,进行自动化压测
查看>>
整站开发初始化
查看>>
洛谷P2900 [USACO08MAR]土地征用Land Acquisition(斜率优化)
查看>>
uoj#448. 【集训队作业2018】人类的本质(Min_25筛+拉格朗日插值)
查看>>
vim配置及插件安装管理(超级详细)
查看>>
楼市仅是阶段性回暖 去库存仍是明年楼市主基调
查看>>
UIImagePickerController
查看>>
怎样打开64位 Ubuntu 的32位支持功能?
查看>>
关于docker jenkins启动时失败的问题处理
查看>>
JavaScript 循环绑定之变量污染
查看>>
poj 1038 Bugs Integrated, Inc. 三进制状态压缩 DFS 滚动数组
查看>>
zoj 1654 Place the Rebots 最大独立集转换成二分图最大独立边(最大匹配)
查看>>
Wordpress解析系列之PHP编写hook钩子原理简单实例
查看>>
怎样看待个体经济
查看>>