SPOJ MINSUB - Largest Submatrix(二分+单调栈)
http://www.spoj.com/problems/MINSUB/en/
题意:给出一个n*m的矩阵M,和一个面积k,要使得M的子矩阵M'的最小元素最大并且面积大于等于k,问子矩阵M'的最小元素最大能是多少,并且求出最大的面积。
思路:二分一个最小元素x,转化为判断矩阵M里面是否存在一个子矩阵使得这个子矩阵的面积大于等于k并且所有元素都大于x。
用另一个矩阵,1表示该位置的元素大于等于x,0表示元素小于x。
转化为判断是否存在一个子矩阵元素为1的面积大于等于k。
这样可以用到早上学习的单调栈,去维护最大的子矩阵面积。

像这样,求出最大的矩阵面积。
如果矩阵面积大于等于k,那么就去找是否有更大的x去满足题意。
#include <bits/stdc++.h>
using namespace std;
#define N 1010
struct node {
int pre, suf, val;
} p[N];
int mat[N][N], mp[N][N], h[N][N], n, m, k, square; bool check(int x) {
memset(mp, , sizeof(mp));
memset(h, , sizeof(h));
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++) if(mat[i][j] >= x) mp[i][j] = ;
square = ;
for(int i = ; i <= n; i++) {
stack<node> sta;
for(int j = ; j <= m; j++) {
if(mp[i][j]) h[i][j] = h[i-][j] + ;
p[j] = (node) {, , h[i][j]};
}
sta.push(p[]);
for(int j = ; j <= m; j++) {
while(!sta.empty() && sta.top().val > p[j].val) {
node top = sta.top(); sta.pop();
if(!sta.empty()) sta.top().suf += top.suf;
p[j].pre += top.pre;
square = max(square, (top.suf + top.pre - ) * top.val);
}
sta.push(p[j]);
}
while(!sta.empty()) {
node top = sta.top(); sta.pop();
if(!sta.empty()) sta.top().suf += top.suf;
square = max(square, (top.suf + top.pre - ) * top.val);
}
}
if(square >= k) return true;
return false;
} void solve() {
int t;
scanf("%d", &t);
while(t--) {
scanf("%d%d%d", &n, &m, &k);
int r = , l = , ans;
for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
scanf("%lld", &mat[i][j]);
if(mat[i][j] < l) l = mat[i][j];
if(mat[i][j] > r) r = mat[i][j];
}
}
ans = l;
while(l <= r) {
int mid = (l + r) >> ;
if(check(mid)) l = mid + , ans = mid;
else r = mid - ;
}
check(ans);
printf("%d %d\n", ans, square);
}
} int main() {
solve();
return ;
}
SPOJ MINSUB - Largest Submatrix(二分+单调栈)的更多相关文章
- MINSUB - Largest Submatrix
MINSUB - Largest Submatrix no tags You are given an matrix M (consisting of nonnegative integers) a ...
- BZOJ 1012--[JSOI2008]最大数maxnumber(二分&单调栈)
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 14142 Solved: 6049[Subm ...
- Gym 100971D Laying Cables 二分 || 单调栈
要求找出每个a[i],找到离他最近而且权值比它大的点,若距离相同,输出权利最大的那个 我的做法有点复杂,时间也要500+ms,因为只要时间花在了map上. 具体思路是模拟一颗树的建立过程,对于权值最大 ...
- 51Nod 1279:扔盘子(二分||单调栈)
1279 扔盘子 1.0 秒 131,072.0 KB 5 分 1级题 有一口井,井的高度为N,每隔1个单位它的宽度有变化.现在从井口往下面扔圆盘,如果圆盘的宽度大于井在某个高度的宽度,则圆盘被卡住( ...
- spoj MINSUB 单调栈+二分
题目链接:点击传送 MINSUB - Largest Submatrix no tags You are given an matrix M (consisting of nonnegative i ...
- POJ-3494 Largest Submatrix of All 1’s (单调栈)
Largest Submatrix of All 1’s Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 8551 Ac ...
- 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 ...
- HDU 2870 Largest Submatrix (单调栈)
http://acm.hdu.edu.cn/showproblem.php? pid=2870 Largest Submatrix Time Limit: 2000/1000 MS (Java/Oth ...
随机推荐
- wpf采用Xps实现文档显示、套打功能
原文:wpf采用Xps实现文档显示.套打功能 近期的一个项目需对数据进行套打,用户要求现场不允许安装office.页面预览显示必须要与文档完全一致,xps文档来对数据进行处理.Wpf的Document ...
- LeapMotion Demo3
原文:LeapMotion Demo3 从Github及其他论坛下载一些LeapMotion的例子,部分例子由于SDK的更新有一些小Bug, 已修复,感兴趣的可以下载: http:// ...
- 用MVVM模式开发中遇到的零散问题总结(4)——自制摄像头拍摄大头贴控件
原文:用MVVM模式开发中遇到的零散问题总结(4)--自制摄像头拍摄大头贴控件 一直有个疑问,为什么silverlight对摄像头支持这么好,WPF却一个库都没有....于是我各种苦恼啊,各种Code ...
- js错误界面
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- Apache Cordova开发环境搭建(一)-Visual Studio
原文:Apache Cordova开发环境搭建(一)-Visual Studio 一.使用Visual Studio开发Apache Cordova手机App 1.版本要求,Visual Studio ...
- 3D-Touch Home Screen Quick Actions 使用
1. 3D-Touch简单介绍 3D-Touch是iPhone 6s推出的一种可以让你与手机进行互动的全新方式.这一次,iPhone 能够感应你按压屏幕的力度.除了轻点.轻扫.双指开合这些熟悉的 Mu ...
- 【Gerrit】Add a Member
add user email:XXXX@163.com username:XXXX( songfei) Add Step: System Server:1. ssh 服务器用户 ...
- .NET MVC 在action中,过滤器中,或视图中,如何分别获取 当前请求的 控制器/视图/区域 的名字
1)过滤器中的: public class CMSAttribute : FilterAttribute, IAuthorizationFilter { public void OnAuthoriza ...
- 如何线程调用C++类成员函数
方法就是: 1,写成静态成员函数 2,参数为 (void* __this)用来传入类 对象指针(this) 3,进入函数首先 C类名 *_this = (C类名*)__this; 转化为对象指 ...
- VS中添加第三方库及相对路径设置
原文 VS中添加第三方库及相对路径设置 对于一些第三方的SDK,一般会包含头文件(*.h),静态库文件(*.lib)和动态库文件(*.dll). 1. 文件位置:为了提高程序的可移植性,将第三库放在 ...