HDU1505(记忆化查找)

本题和1506思路完全一样。只不过需要把图中的点转化为高度,以每行底来查找最大面积*3;

都是利用记忆化查找的思想来查找最左或者最右的边界,具体解释看代码。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <cmath>

using namespace std;
#define cir(a,b) memset(a,b,sizeof a)
const int maxn = 1000+10;
int T;
int m,n;
int s[maxn][maxn];
int h[maxn][maxn];
int l[maxn];
int r[maxn];//l and r 数组是保存每列的最左和最右边的边界的下标
int dp[maxn];
char ch;
int main()
{
    cin >> T;
    while(T--)
    {
        cin >> m >> n;
        for(int i=1; i<=m; i++)
        {
            for(int j=1; j<=n; j++)
            {
                cin >> ch;
                if(ch == 'R') s[i][j] = 0;
                else s[i][j] = 1;
            }
        }
        int number = 0;
        for(int j=1; j<=n; j++)
        {
            number = 0;
            for(int i=1; i<=m; i++)
            {
                if(s[i][j]==1)
                {
                    ++number;
                    h[i][j] = number;
                }
                else
                {
                    number = 0;
                    h[i][j] = number;
                }
            }
        }
//            for(int i=1;i<=m;i++)
//            {
//                for(int j=1;j<=n;j++)
//                {
//                    cout << h[i][j] <<" ";
//                }
//                cout << endl;
//            }
        int Max = -1;
        for(int i=1; i<=m; i++)
        {
            cir(l,0),cir(r,0);//每次清空高度
            
            l[1] = 1;//最左边的边界一定是自己
            
            for(int j=2; j<=n; j++)
            {
                int t1=j;

                while(t1>1&&h[i][t1-1]>=h[i][j])
                {
                    t1 = l[t1-1];// 如果某列的左边的列的高度大于等于本列,所以本列左边的边界一定也是大于等于本列的
                    //cout << t1 <<endl;
                }
                l[j] = t1;//记录最左边界
            }
            
            r[n] = n;最右边的边界一定是自己
            
            for(int j=n-1; j>=1; j--)
            {
                int t2=j;
                
                while(t2<n-1&&h[i][t2+1]>=h[i][j])
                {
                    t2 = r[t2+1];// 如果某列的右边的列的高度大于等于本列,所以本列右边的边界一定也是大于等于本列的
                    // cout << "t2" << " " << t2 <<endl;
                }
                
                r[j] = t2;//记录最右边界
            }
            for(int j=1; j<=n; j++)
            {

                if( (r[j]-l[j]+1) * h[i][j] > Max )
                {
                    //cout << r[j] << " " << l[j] << " "<< h[i][j] << endl;
                    Max = (r[j]-l[j]+1) * h[i][j] ;
                }
            }
        }
        cout << Max*3 << endl;
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值