spoj MINSUB 单调栈+二分
题目链接:点击传送
MINSUB - Largest Submatrix
You are given an matrix M (consisting of nonnegative integers) and an integer K. For any submatrix of M' of M define min(M') to be the minimum value of all the entries of M'. Now your task is simple: find the maximum value of min(M') where M' is a submatrix of M of area at least K (where the area of a submatrix is equal to the number of rows times the number of columns it has).
Input
The first line contains a single integer T (T ≤ 10) denoting the number of test cases, T test cases follow. Each test case starts with a line containing three integers, R (R ≤ 1000), C (C ≤ 1000) and K (K ≤ R * C) which represent the number of rows, columns of the matrix and the parameter K. Then follow R lines each containing C nonnegative integers, representing the elements of the matrix M. Each element of M is ≤ 10^9
Output
For each test case output two integers: the maximum value of min(M'), where M' is a submatrix of M of area at least K, and the maximum area of a submatrix which attains the maximum value of min(M'). Output a single space between the two integers.
Example
Input:
2
2 2 2
1 1
1 1
3 3 2
1 2 3
4 5 6
7 8 9 Output:
1 4
8 2
题意:给你一个n*m的矩阵,求以一个最小值为m的最大矩阵面积s需要大于等于K
思路:二分答案,check怎么写呢。。
类似bzoj 3039这题;
利用单调栈,求最大子矩阵面积;
将check的x,大于等于x的值均改成1,求1的最大面积;
枚举每个位置,以该位置能最大的上升的位置为权值;
例如:
1 0 1 1 0 1
1 1 0 --> 2 1 0
1 0 1 3 0 1
利用单调栈查找 以该权值为最大值最多可以往左和往右延伸最大长度;
详见代码
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e3+,M=1e6+,inf=;
const ll INF=1e18+,mod=1e9+;
int a[N][N],b[N][N];
int l[N],r[N],s[N];
int dp[N][N];
int n,m,k;
int check(int x)
{
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
b[i][j]=(a[i][j]>=x);
memset(dp,,sizeof(dp));
int ans=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
if(b[i][j])dp[i][j]=dp[i-][j]+;
else dp[i][j]=;
dp[i][]=dp[i][m+]=-;
int si=;
s[++si]=;
for(int j=;j<=n;j++)
{
while(dp[i][s[si]]>=dp[i][j])si--;
l[j]=s[si];
s[++si]=j;
}
si=;
s[++si]=m+;
for(int j=m;j>=;j--)
{
while(dp[i][s[si]]>=dp[i][j])si--;
r[j]=s[si];
s[++si]=j;
}
for(int j=;j<=m;j++)
ans=max(ans,(r[j]-l[j]-)*dp[i][j]);
}
return ans;
}
int main()
{
int T,cas=;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&a[i][j]);
int st=;
int en=1e9+,ans=-;
while(st<=en)
{
int mid=(st+en)>>;
if(check(mid)>=k)
{
ans=mid;
st=mid+;
}
else
en=mid-;
}
printf("%d %d\n",ans,check(ans));
}
return ;
}
spoj MINSUB 单调栈+二分的更多相关文章
- BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 8748 Solved: 3835[Submi ...
- bzoj 4709 [Jsoi2011]柠檬——单调栈二分处理决策单调性
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4709 题解:https://blog.csdn.net/neither_nor/articl ...
- BZOJ1012最大数 [JSOI2008] 单调栈+二分
正解:单调栈+二分查找(or,线段树? 解题报告: 拿的洛谷的链接quq 今天尝试学习了下单调栈,然后就看到有个博客安利了这个经典例题?于是就去做了,感觉还是帮助了理解趴quqqqqq 这题,首先,一 ...
- 51NOD 1962 区间计数 单调栈+二分 / 线段树+扫描线
区间计数 基准时间限制:1.5 秒 空间限制:262144 KB 分值: 80 两个数列 {An} , {Bn} ,请求出Ans, Ans定义如下: Ans:=Σni=1Σnj=i[max{ ...
- 【bzoj4237】稻草人 分治+单调栈+二分
题目描述 JOI村有一片荒地,上面竖着N个稻草人,村民们每年多次在稻草人们的周围举行祭典. 有一次,JOI村的村长听到了稻草人们的启示,计划在荒地中开垦一片田地.和启示中的一样,田地需要满足以下条件: ...
- 洛谷P1823 [COI2007] Patrik 音乐会的等待(单调栈+二分查找)
洛谷P1823 [COI2007] Patrik 音乐会的等待(单调栈+二分查找) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1333275 这个题不是很 ...
- 【洛谷P1823】音乐会的等待 单调栈+二分
题目大意:给定一个长度为 N 的序列,定义两个数 \(a[i],a[j]\) 相互看得见,意味着 \(\forall k\in [i+1,j-1],a[k]\le a[i],a[k]\le a[j]\ ...
- BZOJ 2388--旅行规划(分块&单调栈&二分)
2388: 旅行规划 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 405 Solved: 118[Submit][Status][Discuss] ...
- 【BZOJ5083】普及 单调栈+二分+RMQ
[BZOJ5083]普及 Description 有一个长度为n的字符串,每一位只会是p或j.你需要取出一个子串S(从左到右或从右到左一个一个取出),使得 不管是从左往右还是从右往左取,都保证每时每刻 ...
随机推荐
- [LeetCode] 312. Burst Balloons_hard tag: 区间Dynamic Programming
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- 第七章之main函数和启动例程
main函数和启动例程 为什么汇编程序的入口是_start,而C程序的入口是main函数呢?本节就来解释这个问题.在讲例 18.1 “最简单的汇编程序”时,我们的汇编和链接步骤是: $ as hell ...
- 初次使用git上传代码(转)
转自 http://www.cnblogs.com/cxk1995/p/5800196.html 首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我 ...
- 转载自(http://snailz.diandian.com/post/2012-10-24/40041265730)
PHP 5.4.8 添加系统服务命令 之前没注意,PHP 5.4.8 的安装包有自带的系统服务注册文件的 打开编译安装包,换成你自己的路径 cd /mydata/soft/php-5.4.8/ cp ...
- 019-centos的yum用法
1.检测系统是否已经安装过mysql或其依赖:# yum list installed | grep mysql(当然也可以用 rpm -qa | grep mysql) 2.卸载已经存在的mysql ...
- 016-sed
行处理:一次处理一行.正则选定文本 ----->>sed处理格式:一.命令行格式:sed [options] 'command' files(如果没有则是通过管道)1.options: - ...
- VS2010/MFC编程入门之四十四(MFC常用类:定时器Timer)
前面一节鸡啄米讲了CTime类和CTimeSpan类的使用,本节继续讲与时间有关的定时器.定时器并不是一个类,主要考虑到,提起时间的话就不能不说定时器,所以就把它放到CTime和CTimeSpan之后 ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON HomMat2dRotate2
zw版[转发·台湾nvp系列Delphi例程]HALCON HomMat2dRotate2 procedure TForm1.Button1Click(Sender: TObject);var op ...
- Python Data Science Toolbox Part 1 Learning 1 - User-defined functions
User-defined functions from:https://campus.datacamp.com/courses/python-data-science-toolbox-part-1/w ...
- 【VS Hacks】定制VS
# Hack 24 定制快捷键 VS能够做很多键盘的配置,其实在VS中目前已经发现有很多的快捷键了,但是在这个技巧篇里会学到如何创建新的快捷键,以及编辑已有的快捷键.VS中包含很多的命令,只有其中 ...