题目链接:点击传送

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. 用Python实现的数据结构与算法:开篇

    一.概述 用Python实现的数据结构与算法 涵盖了常用的数据结构与算法(全部由Python语言实现),是 Problem Solving with Algorithms and Data Struc ...

  2. X-Forwarded-For 负载均衡 7 层 HTTP 模式获取来访客户端真实 IP 的方法(IIS/Apache/Nginx/Tomcat)

    https://help.aliyun.com/knowledge_detail/13051859.html?pos=1 1.IIS 6 配置方案2.IIS 7 配置方案3.Apache 配置方案4. ...

  3. 浅谈Android View滑动和弹性滑动

    引言 View的滑动这一块在实际开发中是非常重要的,无论是优秀的用户体验还是自定义控件都是需要对这一块了解的,我们今天来谈一下View的滑动. View的滑动 View滑动功能主要可以使用3种方式来实 ...

  4. LeetCode-MinimumDepthOfBinaryTree

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  5. 结合ajax 的表单验证

    浪费了我两天的时间 我也是醉了 html  结构 <!-- 密码修改 --> <div class="modal fade" id="operatePa ...

  6. Gson(Google)基础

    一.所需jar包: gson-x.x.jar(本例使用的是gson-2.7.jar). 二.解析转化: 1.json字符串  < ------ >  json String str=&qu ...

  7. python excel操作单元格复制和读取的两种方法

    操作单元格 新建一个sheet, 单元格赋值(两种方法) 单元格A1赋值为’xiaxiaoxu’ 单元格A2赋值为‘xufengchai’ 打印A1和A2单元格的值(两种方法) #coding=utf ...

  8. MyEclipse2014+JDK1.7+Tomcat8.0+Maven3.2 开发环境搭建

    1.JDK的安装 首先下载JDK,这个从sun公司官网可以下载,根据自己的系统选择64位还是32位,安装过程就是next一路到底.安装完成之后当然要配置环境变量了. ————————————————— ...

  9. String和StringBuffer和StringBuilder

    String类 Java语言中用String类代表不可变的字符串,它是由任意多个字符组成的序列.程序中需要存储大量的信息时,一般都用String对象. 1.字符串初始化 JavaSE API为字符串对 ...

  10. Core Java 1

    p264~p267: 1.程序中可能出现错误:用户输入错误.设备错误.物理限制错误.代码错误 2.如果由于出现错误而使得某些操作没有完成,程序应该:返回一种安全状态,并能够让用户执行一些其他命令: 或 ...