【HDU 1505】 City Game 单调栈

传送门 HDU-1505

题意:找到矩阵中的最大子矩阵(有些点为障碍物)。

思路:
看到网上清一色的dp,我就写一下这个题的单调栈做法。其实这个题和HDU-1506这个题是基本一样的。
1.在1506的那道题里面,我们是在一维上讨论一个最大的矩形区域。如果当前这个点a[i]是我们选中的矩形的最小值的话,那么选中的区间左右两端点旁边的点的大小肯定比a[i]还要小(不然为什么不把它们也加进来呢?)。所以,我们可以先求出每个点往左和往右最近的比它小的值,然后选中a[i]为区域内最小的那个元素的话面积就是(r[i] - l[i] - 1) * a[i] 。
2.放到二维也是如此。我们把每一行看成前面说的一维。高度就是这一行上叠起来的'F'的高度。这个高度我们可以很容易预处理得到。所以代码上就是一个循环遍历行,内层做一个一维的单调栈即可。详见代码。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e3+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline int read(){ int f = 1; int x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

int h[maxn][maxn];
int l[maxn], r[maxn];
char grid[maxn][maxn];
stack<int> s;
int n, m;

void solve_left(int row)
{
    rep(i,0,m+1) l[i] = 0;
    while(!s.empty()) s.pop();
    rep(i,1,m)
    {
        while(!s.empty()&&h[row][s.top()] >= h[row][i]) s.pop();
        if(s.empty()) l[i] = 0;
        else l[i] = s.top();
        s.push(i);
    }
}

void solve_right(int row)
{
    rep(i,0,m+1) r[i] = 0;
    while(!s.empty()) s.pop();
    per(i,m,1)
    {
        while(!s.empty()&&h[row][s.top()] >= h[row][i]) s.pop();
        if(s.empty()) r[i] = m+1;
        else r[i] = s.top();
        s.push(i);
    }

}

int main()
{
    FAST;
    cin.tie(0);
    int kase;
    cin>>kase;
    while(kase--)
    {
        mem(h,0); while(!s.empty()) s.pop();
        n = read(), m = read();

        rep(i,1,n)  rep(j,1,m) cin>>grid[i][j];
        rep(j,1,m) rep(i,1,n) if(grid[i][j] == 'R') h[i][j] = 0; else h[i][j] = h[i-1][j] + 1;
        ll ma = 0;
        rep(i,1,n)
        {
            solve_left(i);
            solve_right(i);
            rep(j,1,m)
            {
                ma = max( (long long)(r[j] - l[j] -1 )*h[i][j], ma);
            }
        }
        cout<<ma*3<<'
';
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Bgwithcode/p/13538915.html