题目链接:https://cn.vjudge.net/problem/HihoCoder-1634

题目意思:可以让矩阵里的某一个数变成p,或者不修改。求最大子矩阵最小,输出最小值。

思路:请看下图

代码:

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <math.h> using namespace std;
typedef long long int LL;
int max(int a,int b,int c,int d)
{
return max(max(a,b),max(c,d));
}
const int maxn=155;
const int INF=2e9+1e8;
int n,m,p;
int a[maxn][maxn],dp[maxn][maxn],ma[maxn][maxn],L[maxn],R[maxn],U[maxn],D[maxn];
void init()
{
memset(L, 0x88, sizeof(L));
memset(R, 0x88, sizeof(R));
memset(U, 0x88, sizeof(U));
memset(D, 0x88, sizeof(D));
}
void deal()
{
int tmp;
// xia
memset(dp, 0, sizeof(dp));
memset(ma, 0x88, sizeof(ma));
tmp = -INF;
for (int i = 1; i <= n; i++)
{
for (int l = 1; l <= m; l++)
{
int sum = 0;
for (int r = l; r <= m; r++)
{
sum += a[i][r];
dp[l][r] += sum;
ma[l][r] = max(ma[l][r], dp[l][r]);
if (dp[l][r] < 0)
dp[l][r] = 0;
tmp = max(tmp, ma[l][r]);
}
}
D[i] = tmp;
}
// zuo
memset(dp, 0, sizeof(dp));
memset(ma, 0x88, sizeof(ma));
tmp = -INF;
for (int i = m; i>0; i--)
{
for (int l = 1; l <= n; l++)
{
int sum = 0;
for (int r = l; r <= n; r++)
{
sum += a[r][i];
dp[l][r] += sum;
ma[l][r] = max(ma[l][r], dp[l][r]);
if (dp[l][r] < 0)
dp[l][r] = 0;
tmp = max(tmp, ma[l][r]);
}
}
L[i] = tmp;
}
// shang
memset(dp, 0, sizeof(dp));
memset(ma, 0x88, sizeof(ma));
tmp = -INF;
for (int i = n; i>0; i--)
{
for (int l = 1; l <= m; l++)
{
int sum = 0;
for (int r = l; r <= m; r++)
{
sum += a[i][r];
dp[l][r] += sum;
ma[l][r] = max(ma[l][r], dp[l][r]);
if (dp[l][r] < 0)
dp[l][r] = 0;
tmp = max(tmp, ma[l][r]);
}
}
U[i] = tmp;
}
// you
memset(dp, 0, sizeof(dp));
memset(ma, 0x88, sizeof(ma));
tmp = -INF;
for (int i = 1; i <= m; i++)
{
for (int l = 1; l <= n; l++)
{
int sum = 0;
for (int r = l; r <= n; r++)
{
sum += a[r][i];
dp[l][r] += sum;
ma[l][r] = max(ma[l][r], dp[l][r]);
if (dp[l][r] < 0)
dp[l][r] = 0;
tmp = max(tmp, ma[l][r]);
}
}
R[i] = tmp;
}
} int main()
{
while(scanf("%d%d%d",&n,&m,&p)+1)
{
init();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
scanf("%d",&a[i][j]);
}
deal();
int ans=D[n];
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
int tmp=max(D[i-1],U[i+1],L[j+1],R[j-1]);
tmp=max(tmp,D[n]-a[i][j]+p);
ans=min(ans,tmp);
}
}
printf("%d\n",ans);
}
return 0;
} /*
3 3 -10
-100 4 4
4 -10 4
4 4 1
3 3 -1
-2 -2 -2
-2 -2 -2
-2 -2 -2 */

Puzzle Game HihoCoder - 1634的更多相关文章

  1. HihoCoder 1634 Puzzle Game(最大子矩阵和)题解

    题意:给一个n*m的矩阵,你只能选择一个格子把这个格子的数换成p(也可以一个都不换),问最大子矩阵和最小可能是多少? 思路: 思路就是上面这个思路,这里简单讲一下怎么n^3求最大子矩阵和:枚举两行(或 ...

  2. Hihocoder 1634 Puzzle Game(2017 ACM-ICPC 北京区域赛 H题,枚举 + 最大子矩阵变形)

    题目链接  2017 Beijing Problem H 题意  给定一个$n * m$的矩阵,现在可以把矩阵中的任意一个数换成$p$,求替换之后最大子矩阵的最小值. 首先想一想暴力的方法,枚举矩阵中 ...

  3. hihoCoder 1426 : What a Ridiculous Election(总统夶选)

    hihoCoder #1426 : What a Ridiculous Election(总统夶选) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - ...

  4. 2015北京网络赛 J Clarke and puzzle 求五维偏序 分块+bitset

    Clarke and puzzle Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc20 ...

  5. hihocoder -1121-二分图的判定

    hihocoder -1121-二分图的判定 1121 : 二分图一•二分图判定 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 大家好,我是小Hi和小Ho的小伙伴Net ...

  6. Hihocoder 太阁最新面经算法竞赛18

    Hihocoder 太阁最新面经算法竞赛18 source: https://hihocoder.com/contest/hihointerview27/problems 题目1 : Big Plus ...

  7. hihoCoder太阁最新面经算法竞赛15

    hihoCoder太阁最新面经算法竞赛15 Link: http://hihocoder.com/contest/hihointerview24 题目1 : Boarding Passes 时间限制: ...

  8. 【hihoCoder 1454】【hiho挑战赛25】【坑】Rikka with Tree II

    http://hihocoder.com/problemset/problem/1454 调了好长时间,谜之WA... 等我以后学好dp再来看为什么吧,先弃坑(╯‵□′)╯︵┻━┻ #include& ...

  9. Puzzle 面向服务/切面(AOP/IOC)开发框架 For .Net

    Puzzle 面向服务/切面AOP开发框架 For .Net AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效 ...

随机推荐

  1. C语言-随机数

    C语言使用rand()函数产生随机数, 使用rand()函数之前要先使用srand(time(0)), 以当前时间作为种子, 否则产生的随机数将不会变化. #include <stdio.h&g ...

  2. Dora.Interception, 为.NET Core度身打造的AOP框架[3]:Interceptor的注册

    在<不一样的Interceptor>中我们着重介绍了Dora.Interception中最为核心的对象Interceptor,以及定义Interceptor类型的一些约定.由于Interc ...

  3. Pimpl Idiom /handle body idiom

    在读<Effective C++>和项目源代码时,看到pImpl Idiom.它可以用来降低文件间的编译依赖关系,通过把一个Class分成两个Class,一个只提供接口,另一个负责实现该接 ...

  4. [WebException: The underlying connection was closed: The message length limit was exceeded.]解决方法

    [WebException: The underlying connection was closed: The message length limit was exceeded.]   Syste ...

  5. CNN学习笔记:卷积神经网络

    CNN学习笔记:卷积神经网络 卷积神经网络 基本结构 卷积神经网络是一种层次模型,其输入是原始数据,如RGB图像.音频等.卷积神经网络通过卷积(convolution)操作.汇合(pooling)操作 ...

  6. Kotlin学习记录2

    参考我的博客:http://www.isedwardtang.com/2017/09/03/kotlin-primer-2/

  7. IDEA MAVEN Project 显示问题

    今天正常打开idea,却发现maven窗口找不到了:试了这些方法 首先idea自带了maven控件,不像Eclipse还需要下载控件,如果你以前有maven在右边,出于某种原因,消失找不到 了,你可以 ...

  8. 面试:做过sql优化吗?

    近来面试找工作经常会遇见这种问题: 做过数据库优化吗?大数据量基础过吗?系统反应慢怎么查询? 这咱也没背过啊,面试还老问,现在的网站主要的压力都来自于数据库,频繁的数据库访问经常会使系统瘫痪,这样就需 ...

  9. 20145222黄亚奇《网络对抗》- shellcode注入&Return-to-libc攻击深入

    20145222黄亚奇<网络对抗>- shellcode注入&Return-to-libc攻击深入 shellcode注入实践过程

  10. 什么是“欧几里德范数”(Euclidean norm)?

    x是n维向量(x1,x2,…,xn),||x||=根号(|x1|方+|x2|方+…+|xn|方) 补充:开平方,跟几何一样