题目链接:(vjudge) 戳我

和上面那个matrix 比较像。

大概题意就是给你一个n*m的矩阵,然后可以选择其中一个数字进行修改(当然也可以不修改),使得矩阵的最大子矩阵尽可能小。最后输出这个值。

我们\(n^2\)遍历i,j,对于一个位置\([i,j]\)看看如果对它进行更改结果会如何。显然如果p值大于该节点的数,我们可以选择不更改。

我们考虑修改了位置为i,j的一个数后,剩下的最大子矩阵会有什么情况?

这里进行分类讨论,第一类是不选择这个数字了。之后最大子矩阵就有“在它左边”,“在它右边”,“在它上面”,“在它下面”四种情况了。而这四种情况对应的最大子矩阵和显然我们可以通过预处理计算出来。

第二类是仍然选择这个数字,这个比较简单,直接对原来矩阵的最大子矩阵和进行更改即可。

代码如下:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
#define MAXN 310
using namespace std;
int n,m,p,ans,ansans,all;
int a[MAXN][MAXN],up[MAXN],down[MAXN],ll[MAXN],rr[MAXN],sum[MAXN],dp[MAXN];
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
while(scanf("%d%d%d",&n,&m,&p)!=EOF)
{
memset(a,0,sizeof(a));
for(int i=0;i<=300;i++)
ll[i]=rr[i]=up[i]=down[i]=-0x3f3f3f3f;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&a[i][j]);
memset(dp,0,sizeof(dp));
for(int l=1;l<=n;l++)
{
memset(sum,0,sizeof(sum));
for(int r=l;r<=n;r++)
{
int cur_ans=-0x3f3f3f3f;
for(int i=1;i<=m;i++)
{
if(l==r) sum[i]=a[r][i];
else sum[i]+=a[r][i];
dp[i]=max(dp[i-1]+sum[i],sum[i]);
cur_ans=max(dp[i],cur_ans);
}
for(int i=r;i<=n;i++) up[i]=max(up[i],cur_ans);
for(int i=1;i<=l;i++) down[i]=max(down[i],cur_ans);
}
}
memset(dp,0,sizeof(dp));
for(int l=1;l<=m;l++)
{
memset(sum,0,sizeof(sum));
for(int r=l;r<=m;r++)
{
int cur_ans=-0x3f3f3f3f;
for(int i=1;i<=n;i++)
{
if(l==r) sum[i]=a[i][r];
else sum[i]+=a[i][r];
dp[i]=max(dp[i-1]+sum[i],sum[i]);
cur_ans=max(cur_ans,dp[i]);
}
for(int i=r;i<=m;i++) ll[i]=max(ll[i],cur_ans);
for(int i=1;i<=l;i++) rr[i]=max(rr[i],cur_ans);
}
}
//for(int i=1;i<=n;i++)printf("i=%d ll=%d rr=%d up=%d down=%d\n",i,ll[i],rr[i],up[i],down[i]);
ansans=up[n];
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i][j]<p) continue;
ans=-0x3f3f3f3f;
ans=max(ans,max(max(up[i-1],down[i+1]),max(ll[j-1],rr[j+1])));
ans=max(ans,down[1]-a[i][j]+p);
ansans=min(ansans,ans);
}
}
printf("%d\n",ansans);
}
}

hihocoder1634 Puzzle Game的更多相关文章

  1. 【Hihocoder1634】Puzzle Game(DP)

    题意:有一个n*m的矩阵,每个矩阵里有一个数字a[i][j].现在要求将其中一个格子的值改为p,使得修改后矩阵的最大子矩阵和最小,求这个最小值 n,m<=150,abs(a[i][j])< ...

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

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

  3. HDU5456 Matches Puzzle Game(DP)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5456 Description As an exciting puzzle game for ...

  4. one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏

    one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle. reference: t ...

  5. poj3678 Katu Puzzle 2-SAT

    Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6714   Accepted: 2472 Descr ...

  6. POJ1651Multiplication Puzzle[区间DP]

    Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8737   Accepted:  ...

  7. codeforce B Island Puzzle

    B. Island Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  8. poj 1651 Multiplication Puzzle (区间dp)

    题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...

  9. Ignatius's puzzle

    Ignatius's puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. nextcloud 安装

    nextcloud 优化 设置php.ini vim /etc/php/7.0/apache2/php.ini 添加以下代码: opcache.enable= opcache.enable_cli= ...

  2. Python OrderedDict使用

    一.最近最少使用实现: import collections class LRUDict(object): ''' 最近最少使用队列实现,最近使用的键值放后面 ''' def __init__(sel ...

  3. iOS中NSDate常用转换操作整合

    //当前时间格式化, 例:YYYY-MM-dd-EEEE-HH:mm:ss + (NSString *)getCurrentDataWithDateFormate:(NSString *)format ...

  4. MongoDB 数组操作

    $push:向文档数组中添加元素,如果没有该数组,则自动添加数组.db.users.insert({"name":"zhang"})db.users.updat ...

  5. Lunix7 开放指定端口

    systemctl unmask firewalld.service 此时可以正常启动Firewall了. 接下来顺便讲述一下Firewall的安装,及一些简单配置.   查看状态,看电脑上是否已经安 ...

  6. zabbix自定义key监控redis

    一.启动redis-server cd /data/redis redis-server redis.conf (根据自己的环境启动redis) 测试脚本(写入1000个数据): seq |while ...

  7. javascript的constructor属性

    /* constructor 属性 constructor 属性返回所有 JavaScript 变量的构造函数. */console.log("John".constructor) ...

  8. 使用python进行汉语分词-乾颐堂

    目前我常常使用的分词有结巴分词.NLPIR分词等等 最近是在使用结巴分词,稍微做一下推荐,还是蛮好用的. 一.结巴分词简介 利用结巴分词进行中文分词,基本实现原理有三: 基于Trie树结构实现高效的词 ...

  9. N-Gram的数据结构

    ARPA的n-gram语法如下: [html] view plaincopyprint? \data\ ngram 1=64000 ngram 2=522530 ngram 3=173445 \1-g ...

  10. HttpClient 上传/下载文件计算文件传输进度

    1.使用ProgressMessageHandler 获取进度 using namespace System.Net.Http; HttpClientHandler hand = new HttpCl ...