SPOJ MINSUB 二分+单调栈
来源:互联网 发布:yum repolist卡死 编辑:程序博客网 时间:2024/06/06 04:03
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:22 2 21 11 13 3 21 2 34 5 67 8 9Output:1 48 2
题解:二分答案 对于每次二分 创造出一个新的01矩阵 1代表当前为比此答案大 0代表小 问题就转化成了最大01子矩阵
用单调栈求解子矩阵
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int n,m,k;int a[1005][1005],b[1005][1005],d[1005],lef[1005][1005],rig[1005][1005];int solve(int x) { int i,j,ans;ans=0; for(j=1;j<=m;j++)b[1][j]=(a[1][j]>=x); for(i=2;i<=n;i++)for(j=1;j<=m;j++)b[i][j]=(a[i][j]>=x)?(1+b[i-1][j]):0; for(i=1;i<=n;i++) { d[0]=1; d[1]=lef[i][1]=1; for(j=2;j<=m;j++) { while(d[0]>0&&b[i][d[d[0]]]>=b[i][j])d[0]--; lef[i][j]=(d[0])?(d[d[0]]+1):1;d[++d[0]]=j; } d[d[0]=1]=rig[i][m]=m; for(j=m-1;j>=1;j--) { while(d[0]>0&&b[i][d[d[0]]]>=b[i][j])d[0]--; rig[i][j]=(d[0])?(d[d[0]]-1):m;d[++d[0]]=j; } for(j=1;j<=m;j++)if(b[i][j])ans=max(ans,b[i][j]*(rig[i][j]-lef[i][j]+1)); } return (ans>=k)?ans:0; } int main(){int t,i,j;scanf("%d",&t);while(t--){scanf("%d%d%d",&n,&m,&k);for(i=1;i<=n;i++){for(j=1;j<=m;j++)scanf("%d",&a[i][j]);}int l=0,r=1000000000;while(l+1<r){int mid=(l+r)/2;if(solve(mid))l=mid;else r=mid;}if(solve(r))l=r;printf("%d %d\n",l,solve(l));}return 0;}
- 二分+单调栈 SPOJ MINSUB
- SPOJ MINSUB 二分+单调栈
- SPOJ-MINSUB - Largest Submatrix(单调栈,二分,好题)
- [SPOJ MINSUB Largest Submatrix]单调栈
- SPOJ MINSUB
- SPOJ-MINSUB
- SPOJ MINSUN Largest Submatrix [二分+单调栈]
- SPOJ 20863 MINSUB
- Wannafly2016-12-27 SPOJ-MINSUB
- SPOJ DRUIDEOI (单调栈)
- spoj-3415(后缀数组+单调栈)
- SPOJ_MINSUB:Largest_Submatrix(二分+单调栈)
- SPOJ_MINSUB:Largest_Submatrix(二分+单调栈)
- spoj:DRUIDEOI - Fata7y Ya Warda!(单调栈)
- 【SPOJ-ARRAYSUB】subarrays【单调队列】
- poj 2796 st算法+二分 / 单调栈
- SPOJ AGGRCOW 二分枚举
- SPOJ - PHT【二分+预处理】
- java socket远程控制主机,执行命令
- android中读取properties文件
- jquery1.9 动态修改select的值
- Java基础之抽象类与接口
- 卡夫卡
- SPOJ MINSUB 二分+单调栈
- 解决超出打开游标的最大数异常ORA-01000 递归SQL 级别1 出现错误 最全方案-最全方案
- Sass的编译和单文件转换
- 音频知识之MP3文件结构解析
- 【JAVA秒会技术之异常解决】怎么解决eclipse报PermGen space异常的问题
- Activiti数据表结构
- 关于freemarker中map的key只能是String的解决办法。
- 【Algothrim】 strcmp strcpy
- DIJKSTRA程序算法示例