Largest Submatrix SPOJ - MINSUB (单调栈)

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

给你一个矩阵让你求 一个矩阵的最小值最大并且面积也是最大,二分最小值,把大于等于最小值的数定义为1,小于的定义为0,那么就变成了求最大的01矩阵问题,这个问题用单调栈可以on解决,首先把其中一维的最常连续长度求得,然后遍历的时候往左延伸到最长和往右延伸到最长,这个地方可以用单调栈来处理,就是比当前数大的不要,比当前数小的入栈,因为当前节点无法延伸到比自己小的长度。这样 (R[i]-L[i]-1)*f[i][j] 就是最大的延伸长度。由于R[i]和L[i]都是不能触及的 所以是-1。

#include <bits/stdc++.h>
using namespace std;
int n,m,k;

int mp[1010][1010];
int mn,mx;
int f[1010][1010];
int s[1010],l[1010],r[1010];
int  total;
int check(int x)
{
    int ans=0;
    memset(f,0,sizeof(f));
    memset(l,0,sizeof(l));
    memset(r,0,sizeof(r));
    for(int i=1;i<=m;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(mp[j][i]>=x)
                f[j][i]=f[j-1][i]+1;
        }
    }
    int top=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            int now=j-1;
            while(now&&f[i][now]>=f[i][j])
                now=l[now];
            l[j]=now;
        }
        for(int j=m;j>=1;j--)
        {
            int now=j+1;
            while(now!=m+1&&f[i][now]>=f[i][j])
                now=r[now];
            r[j]=now;
        }
        for(int j=1;j<=m;j++)
        {
            ans=max(ans,(r[j]-l[j]-1)*f[i][j]);
        }
    }
    if(ans>=k) 
    {
        return ans;
    }
    else return 0;
}


int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        total=0;
        mn=1e9+7;
        mx=0;
        scanf("%d%d%d",&n,&m,&k);
        memset(mp,0,sizeof(mp));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&mp[i][j]);
                mn=min(mp[i][j],mn);
                mx=max(mp[i][j],mx);
            }
        }
        int l=mn,r=mx;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            if(check(mid)) l=mid+1;
            else r=mid-1;
        }
        printf("%d %d\n",r,check(r));
    }
}
posted @ 2017-09-19 21:18  黑码的博客  阅读(100)  评论(0编辑  收藏  举报