【POJ 1964】 City Game

【题目链接】

           http://poj.org/problem?id=1964

【算法】

            记f[i]表示第i行最多向上延伸的行数

            然后,对于每一行,我们用单调栈计算出这一行向上延伸的最大矩形面积,取最大值,即可

【代码】

          

#include <algorithm>  
#include <bitset>  
#include <cctype>  
#include <cerrno>  
#include <clocale>  
#include <cmath>  
#include <complex>  
#include <cstdio>  
#include <cstdlib>  
#include <cstring>  
#include <ctime>  
#include <deque>  
#include <exception>  
#include <fstream>  
#include <functional>  
#include <limits>  
#include <list>  
#include <map>  
#include <iomanip>  
#include <ios>  
#include <iosfwd>  
#include <iostream>  
#include <istream>  
#include <ostream>  
#include <queue>  
#include <set>  
#include <sstream>  
#include <stdexcept>  
#include <streambuf>  
#include <string>  
#include <utility>  
#include <vector>  
#include <cwchar>  
#include <cwctype>  
#include <stack>  
#include <limits.h> 
using namespace std;
#define MAXN 1010

int T,n,m,i,j,ans;
int f[MAXN][MAXN];
char tmp[5];

inline int getans(int *a)
{
        int i,top = 0,ans = 0,tmp;
        static int s[MAXN],w[MAXN];
        s[0] = a[m+1] = -1;
        for (i = 1; i <= m + 1; i++)
        {
                if (a[i] >= s[top]) 
                {
                        s[++top] = a[i];
                        w[top] = 1;        
                }    else
                {
                        tmp = 0;
                        while (a[i] < s[top])
                        {
                                tmp += w[top];
                                ans = max(ans,tmp*s[top]);
                                top--;
                        }
                        s[++top] = a[i];
                        w[top] = tmp + 1;
                } 
        }        
        return ans;
}

int main() 
{
        
        scanf("%d",&T);
        while (T--)
        {
                ans = 0;
                scanf("%d%d",&n,&m);
                for (i = 1; i <= n; i++)
                {
                        for (j = 1; j <= m; j++)
                        {
                                scanf("%s",&tmp);
                                f[i][j] = (tmp[0] == 'F') ? (f[i-1][j] + 1) : (0);
                        }
                }
                for (i = 1; i <= n; i++) ans = max(ans,getans(f[i]));
                printf("%d
",ans*3);
        }
        
        return 0;
    
}
原文地址:https://www.cnblogs.com/evenbao/p/9254134.html