hdu 1505 City Game (hdu 1506的升级版)

想象成hdu 1506的图

预处理一遍得到所有点的高度,然后对每一行做一次hdu 1506的算法。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
char map[1005][1005];
int h[1005][1005];
int l[1005];
int r[1005];
int n,m;
int main()
{
    int cas;
    scanf("%d",&cas);
    char w;
    while(cas--)
    {
        scanf("%d%d",&n,&m);    
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf(" %c",&w);
                map[i][j]=w;
            }
        }
        for(int i=1;i<=n;i++)       //h得到每个位置的高度。
        {
            for(int j=1;j<=m;j++)
            {
                h[i][j] = map[i][j]=='F'?h[i-1][j]+1:0;
            }
        }
        for(int i=1;i<=n;i++)       //对每一行做一次 hdu1506 的做法
        {
            h[i][0]=h[i][m+1]=-1;
            int tmp;
            for(int j=1;j<=m;j++)
            {
                tmp=j;
                while(h[i][tmp-1]>=h[i][j]) tmp=l[tmp-1];
                l[j]=tmp;
            }
            for(int j=m;j>=1;j--)
            {
                tmp=j;
                while(h[i][tmp+1]>=h[i][j]) tmp=r[tmp+1];
                r[j]=tmp;
            }
            for(int j=1;j<=m;j++)
            {
                ans=max(ans,h[i][j]*(r[j]-l[j]+1));
            }
        }
        printf("%d\n",ans*3);
    }
    return 0;
}


你可能感兴趣的:(hdu 1505 City Game (hdu 1506的升级版))