MINSUB - Largest Submatrix
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
分析:首先二分答案M,其次改写成01矩阵,这样变成求最大的全1子矩阵;
最大全1子矩阵那么可以单调栈解决;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e3+;
const int N=1e3+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,qu[maxn],a[maxn][maxn],v[maxn][maxn],le[maxn],ret,ma,now;
bool ok(int x)
{
now=;
int i,j;
rep(i,,n)rep(j,,m)v[i][j]=(a[i][j]>=x?v[i-][j]+:);
rep(i,,n)
{
rep(j,,m+)
{
if(!qu[])qu[++qu[]]=v[i][j],le[qu[]]=j;
else if(v[i][j]>qu[qu[]])qu[++qu[]]=v[i][j],le[qu[]]=j;
else if(v[i][j]<qu[qu[]])
{
int tmp;
while(qu[]>=&&v[i][j]<=qu[qu[]])
{
now=max(now,(j-le[qu[]])*qu[qu[]]);
tmp=le[qu[]];
qu[]--;
}
qu[++qu[]]=v[i][j];
le[qu[]]=tmp;
}
}
qu[]--;
}
return now>=k;
}
int main()
{
int i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
rep(i,,n)rep(j,,m)scanf("%d",&a[i][j]);
int l=,r=1e9;
while(l<=r)
{
int mid=l+r>>;
if(ok(mid))ret=mid,ma=now,l=mid+;
else r=mid-;
}
printf("%d %d\n",ret,ma);
}
return ;
}
MINSUB - Largest Submatrix的更多相关文章
- SPOJ MINSUB - Largest Submatrix(二分+单调栈)
		http://www.spoj.com/problems/MINSUB/en/ 题意:给出一个n*m的矩阵M,和一个面积k,要使得M的子矩阵M'的最小元素最大并且面积大于等于k,问子矩阵M'的最小元素 ... 
- Largest Submatrix(动态规划)
		Largest Submatrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ... 
- POJ-3494 Largest Submatrix of All 1’s (单调栈)
		Largest Submatrix of All 1’s Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 8551 Ac ... 
- hdu 2870 Largest Submatrix(平面直方图的最大面积 变形)
		Problem Description Now here is a matrix with letter 'a','b','c','w','x','y','z' and you can change ... 
- Largest Submatrix of All 1’s
		Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest we m ... 
- codeforces 407D Largest Submatrix 3
		codeforces 407D Largest Submatrix 3 题意 找出最大子矩阵,须满足矩阵内的元素互不相等. 题解 官方做法 http://codeforces.com/blog/ent ... 
- Largest Submatrix of All 1’s(思维+单调栈)
		Given a m-by-n (0,1)-matrix, of all its submatrices of all 1's which is the largest? By largest we m ... 
- POJ 3494 Largest Submatrix of All 1’s 单调队列||单调栈
		POJ 3494 Largest Submatrix of All 1’s Description Given a m-by-n (0,1)-matrix, of all its submatrice ... 
- POJ - 3494 Largest Submatrix of All 1’s 单调栈求最大子矩阵
		Largest Submatrix of All 1’s Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is ... 
随机推荐
- RDA 字库制作
			制作韩语字库为例: 1.韩语UNICODE 范围 TV_IDF_uni_korean.txt [01fa,] [02c6,02c7] [02c9,02ca] [02cd,02cd] [02d8,02d ... 
- ubuntu16.04 Kafka 安装
			Kafka核心概念: 下面介绍Kafka相关概念,以便运行下面实例的同时,更好地理解Kafka. 1. Broker Kafka集群包含一个或多个服务器,这种服务器被称为broker 2. Topic ... 
- PCB Genesis脚本 C#调用Javascript
			曾经用node.js测试写Genesis脚本失败了,这次借助开发PCB规则引擎的机会(基于JS V8引擎与.net深度交互性), 验证一下Javascript是否可用于写Genesis脚本. 一.测试 ... 
- Akka源码分析-Remote-Creating Actors Remotely
			在akka官网中关于远程actor交互,介绍了两种方法,一种是通过actorSelection查询,另一种是通过actorOf在远程节点创建一个actor.actorSelection我们之前的博客中 ... 
- 动态规划DP入门
			百度百科↓ 动态规划(dynamic programming)是运筹学的一个分支,是求解决策过程(decision process)最优化的数学方法.20世纪50年代初美国数学家R.E.Bellman ... 
- SCOI2014题解
			SCOI2014由极不靠谱的电子坑爹大学出题.电子坑爹大学打破了多年行规,暴力+NOIP500分居然不能进队.极其逗比的第一天暴力给10分!!还好有些题不是很难,省队爷分数大概在150以上(最高420 ... 
- Android热修复方案比较
			热修复的特点:无需重新发版,实时高效热修复:用户无感知修复,无需下载新的应用,代价小: 修复成功率高,把损失降到最低. 一.热修复开源方案和使用情况 方案名称 方案开发公司 开发时间 Github星评 ... 
- Glide4.0 centerCrop属性和圆角 冲突
			首先致谢:https://blog.csdn.net/flyinbed_/article/details/75506062 咱们不是代码的生产者,只是代码的搬运工. 最近有个工作中有个需求就是展示的图 ... 
- php中的抽象方法和抽象类
			1.什么是抽象方法? 我们在类里面定义的没有方法提的方法就是抽象方法.所谓的没有方法体指的是,在声明的时候没有大括号以及其中的内容,而是直接在声明时在方法名后加上分号结束,另外在声明抽象方法时方 ... 
- win7 硬盘安装suse双系统启动顺序更改
			使用win7硬盘安装suse双系统之后,首先面临的问题是,PC默认启动的系统更改的问题,有些人可能想默认启动是win7,只有在使用linux的时候在去选择suse系统,这里我告诉大家更改的办法: 首先 ... 
