题目链接:点击传送

MINSUB - Largest Submatrix

no tags 

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 单调栈+二分的更多相关文章

  1. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

  2. bzoj 4709 [Jsoi2011]柠檬——单调栈二分处理决策单调性

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4709 题解:https://blog.csdn.net/neither_nor/articl ...

  3. BZOJ1012最大数 [JSOI2008] 单调栈+二分

    正解:单调栈+二分查找(or,线段树? 解题报告: 拿的洛谷的链接quq 今天尝试学习了下单调栈,然后就看到有个博客安利了这个经典例题?于是就去做了,感觉还是帮助了理解趴quqqqqq 这题,首先,一 ...

  4. 51NOD 1962 区间计数 单调栈+二分 / 线段树+扫描线

     区间计数   基准时间限制:1.5 秒 空间限制:262144 KB 分值: 80   两个数列 {An} , {Bn} ,请求出Ans, Ans定义如下: Ans:=Σni=1Σnj=i[max{ ...

  5. 【bzoj4237】稻草人 分治+单调栈+二分

    题目描述 JOI村有一片荒地,上面竖着N个稻草人,村民们每年多次在稻草人们的周围举行祭典. 有一次,JOI村的村长听到了稻草人们的启示,计划在荒地中开垦一片田地.和启示中的一样,田地需要满足以下条件: ...

  6. 洛谷P1823 [COI2007] Patrik 音乐会的等待(单调栈+二分查找)

    洛谷P1823 [COI2007] Patrik 音乐会的等待(单调栈+二分查找) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1333275 这个题不是很 ...

  7. 【洛谷P1823】音乐会的等待 单调栈+二分

    题目大意:给定一个长度为 N 的序列,定义两个数 \(a[i],a[j]\) 相互看得见,意味着 \(\forall k\in [i+1,j-1],a[k]\le a[i],a[k]\le a[j]\ ...

  8. BZOJ 2388--旅行规划(分块&单调栈&二分)

    2388: 旅行规划 Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 405  Solved: 118[Submit][Status][Discuss] ...

  9. 【BZOJ5083】普及 单调栈+二分+RMQ

    [BZOJ5083]普及 Description 有一个长度为n的字符串,每一位只会是p或j.你需要取出一个子串S(从左到右或从右到左一个一个取出),使得 不管是从左往右还是从右往左取,都保证每时每刻 ...

随机推荐

  1. Ubuntu搭建solr搜索服务器

    参考:http://blog.csdn.net/makang110/article/details/50971705 一:搭建solr服务器 1:安装jdk1.7,并配置环境变量 2:下载tomcat ...

  2. [LeetCode] 181. Employees Earning More Than Their Managers_Easy tag: SQL

    The Employee table holds all employees including their managers. Every employee has an Id, and there ...

  3. 关闭WPS启动时显示的在线模板

    关闭WPS启动时显示的在线模板 以WPS2016为例,如下图: 操作步骤如下: ①点击在线模板页面右下方的"设置"选项 ②"设置"界面打开后,选择启动WPS默认 ...

  4. 将Mongo装为Windows的服务

    PHP的mongo扩展: 首先 下载一个PHP的mongo扩展, 地址:http://download.csdn.net/detail/qq_36387589/9819259 然后修改php.ini. ...

  5. dxf cad dwg 文件读写,支持跨平台移植 库

    http://www.pudn.com/Download/item/id/3096684.html 联合开发网 --->绘图程序

  6. C# 多线程,new ThreadStart(方法)中的方法如果有参数,该怎么写

    using System; using System.Threading; public class Work { public static void Main() { // Start a thr ...

  7. sql server2012重复执行创建表视图sql及带行号的视图

    1.可重复操作(创建表,视图)的sql语句判断 IF EXISTS ( SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[re ...

  8. 代码静态检查Eclipse插件:SonarLint插件离线安装

    Eclipse Version: Oxygen.3a Release (4.7.3a)Myeclipse版本: 10.7 SonarLint 插件离线安装包:org.sonarlint.eclipse ...

  9. VMWare中桥接、NAT、Host-only

    1.概述 2.bridged(桥接模式) 3.NAT(网络地址转换模式) 4.host-only(主机模式) 5.replicate physical network connection state ...

  10. Linux基础命令---gzip

    gzip gzip通过Lempel-ziv算法来压缩文件,压缩的时候保留每个文件的所有者.权限.修改时间.对于符号链接,gzip将会忽略它. 如果压缩的文件名对其文件系统来说太长,则gzip将截断它. ...