POJ1964-City Game
给你N×M大的矩阵,里面分别有字符‘F'和’R',要找到一个最大的只有‘F'的矩阵,不能包含有’R‘。N,M<=1000。
一开始的思路是单调栈来求最大矩形面积,因为没看清题目不能包含’R'字符,所以算出每行的‘F'字符个数然后单调栈就WA了。。
然后想到要从左边开始,算出连续的‘F'字符个数,然后又WA了。
因为还有右边,所以右边开始的’F'字符也处理一下,也是WA。
接着想到这种情形:
R F F F R
R F F F R
这枚举F的开始结束的话都是不行的,那么因为就两种字符。然后我又上一步一样只是求出R连续的大小,然后统计
lf左边开始‘F'的连续个数然后单调栈
rf右边开始’F'的连续个数然后单调栈
lr左边开始‘R'的连续个数然后单调栈
rr右边开始’R'的连续个数然后单调栈。
那么答案我就觉得是max(lf,rf,n*m-max(lr,rr))
然后也WA,因为我想到一个反例:
R R R R R
R F F F R
R F F F R
R F F F R
R R R R R
仔细观察这个样例,发现其实之前的做法很麻烦,我们最后得出的矩形一定是有左边的(废话),但是这个左边不确定,只要确定了直接用单调栈就出来了。
所以想到可以枚举K列,每次从第K列开始算。代码如下:
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std; int n,m,arr[],stk[],w[];
char ma[][],ss[];
long long ans;
int main() {
int k;
scanf("%d",&k);
while(k--) {
long long lr,rr,lf,rf;
lr=rr=lf=rf=;
memset(stk,,sizeof stk);
ans=;
memset(w,,sizeof w);
scanf("%d%d",&n,&m);
for(int i=; i<=n; ++i) {
for(int j=; j<=m; ++j)
scanf("%s",ss),ma[i][j]=ss[]; }
for(int k=; k<=m; ++k) {
for(int i=; i<=n; ++i) {
int tmp=;
int j=k;
while(j<=m&&ma[i][j++]!='R')
++tmp;
/*
for(int j=1;j<=m;++j)
if(ma[i][j]=='F')
++tmp;
*/
arr[i]=tmp;
}
memset(stk,,sizeof stk);
memset(w,,sizeof w);
lf=;
arr[n+]=;
int p=;
for(int i=; i<=n+; ++i) {
if(arr[i]>stk[p])
stk[++p]=arr[i],w[p]=;
else {
int wid=;
while(stk[p]>arr[i]) {
wid+=w[p];
lf=max(lf,(long long)wid*stk[p]);
--p;
}
stk[++p]=arr[i];
w[p]=wid+;
}
}
ans=max(ans,lf);
}
printf("%lld\n",ans*);
}
return ;
}
POJ1964-City Game的更多相关文章
- [POJ1964]City Game (悬线法)
题意 其实就是BZOJ3039 不过没权限号(粗鄙之语) 同时也是洛谷4147 就是求最大子矩阵然后*3 思路 悬线法 有个博客讲的不错https://blog.csdn.net/u012288458 ...
- 题解 POJ1964/UVA1330/SP277 【City Game】
题目链接: https://www.luogu.org/problemnew/show/UVA1330 http://poj.org/problem?id=1964 https://www.luogu ...
- BZOJ 2001: [Hnoi2010]City 城市建设
2001: [Hnoi2010]City 城市建设 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1132 Solved: 555[Submit][ ...
- History lives on in this distinguished Polish city II 2017/1/5
原文 Some fresh air After your time underground,you can return to ground level or maybe even a little ...
- History lives on in this distinguished Polish city 2017/1/4
原文 History lives on in this distinguished Polish city Though it may be ancient. KraKow, Poland, is a ...
- #1094 : Lost in the City
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi gets lost in the city. He does not know where he is ...
- GeoIP Legacy City数据库安装说明
Here is a brief outline of the steps needed to install GeoIP Legacy City on Linux/Unix. The installa ...
- [POJ3277]City Horizon
[POJ3277]City Horizon 试题描述 Farmer John has taken his cows on a trip to the city! As the sun sets, th ...
- 2015年第8本(英文第7本):the city of ember 微光城市
书名:the City of Ember(中文名:微光城市) 作者:Jeanne DuPrau 单词数:6.2万 不重复单词数:未知 首万词不重复单词数:未知 蓝思值:未知 阅读时间:2015年4月2 ...
- 离散化+线段树 POJ 3277 City Horizon
POJ 3277 City Horizon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18466 Accepted: 507 ...
随机推荐
- 第九篇 Flask 中的蓝图(BluePrint)
第九篇 Flask 中的蓝图(BluePrint) 蓝图,听起来就是一个很宏伟的东西 在Flask中的蓝图 blueprint 也是非常宏伟的 它的作用就是将 功能 与 主服务 分开怎么理解呢? ...
- C# Common Code
DatePicker 控件日期格式化,可以在App.xaml.cs中添加下面代码 方法一 不推荐: Thread.CurrentThread.CurrentCulture = (CultureInfo ...
- C++中为什么有时要使用extern "C"
extern "C"的作用 在C++引用lua的头文件时,我们总会写成: extern "C" { #include "lua.h" #in ...
- 变邻域搜索(Variable neighborhood search)
变邻域搜索(Variable neighborhood search)VNS是Hansen等提出的一种元启发近似算法,它通过在不同的邻域结构内跳转搜索, 能够避免陷入局部最优解. 算法主要分为两部分: ...
- CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-5安装JDK及安装mysql数据库
1.安装JDK 可以不用卸载自带的openjdk,配好环境变量即可. 下载文件:jdk-8u151-linux-x64.tar.gz 附:JDK各版本下载地址:https://www.oracle.c ...
- centos7 安装部署gitlab
Gitlab官网地址:https://about.gitlab.com/downloads/ Linux系统环境: Centos7 gitlab服务安装之前需要安装一些依赖包:yum install ...
- mysql中文查询问题
alter table t_foo change `str` `str` varchar(100) character set utf8 not null ;
- 快速排序的两种实现方法(js)
快速排序的基本思想:通过一趟排序,将待排记录分割成独立的两部分,其中一部分记录的关键字均比另外一部分记录的关键字小,则可分别对着两部分记录继续进行排序,以达到整个序列有序的目的.----------- ...
- SystemUI中监听app启动,修改app中的状态栏背景色
参考 http://www.2cto.com/kf/201206/137225.html 从Android4.4开始,app可以自定义status bar 背景. 对于一些第三方app定义的状态栏背景 ...
- Siamese Neural Networks for One-shot Image Recognition
one-shot learning简介 这是迁移学习的两种极端形式 zero-shot learning 指的是我们之前没有这个类别的训练样本,但是我们可以学习到一个映射X->Y, 如果这个映射 ...