ArcTan

dfs
随笔 - 16, 文章 - 117, 评论 - 6, 引用 - 0
数据加载中……

poj1694(单调栈-求矩形最大面积)

今天就做做单调栈,挺好写的。应该多了解了原理
字符串读入还是有问题啊,这个不行,得看清楚输入后在写!
一次AC:
#include<stdio.h>
#include
<string.h>
#include
<math.h>
int n,m,ans;
int a[1005],stack[1005],pos[1005];
int GetAns()
{
    
int i,top;
    top
=0;
    stack[
0]=pos[0]=0;
    
for (i=1;i<=n ;i++ )
    {
        
while (top>0&&a[i]<=stack[top])
        {
            
if (ans<(i-1-pos[top-1])*stack[top])
                ans
=(i-1-pos[top-1])*stack[top];
            top
--;
        }
        top
++;
        stack[top]
=a[i];
        pos[top]
=i;
    }
    
while (top>0)
    {
        
if (ans<(n-pos[top-1])*stack[top])
            ans
=(n-pos[top-1])*stack[top];
        top
--;
    }
}
int main()
{
    
int cas,i,j;
    
char ch[2];
    scanf(
"%d",&cas);
    
while (cas--)
    {
        memset(a,
0,sizeof(a));
        ans
=0;
        scanf(
"%d%d",&m,&n);
        
for (i=0;i<m ;i++ )
        {
            
for (j=1;j<=n ;j++ )
            {
                scanf(
"%s",&ch);
                
if (ch[0]=='F')
                    a[j]
++;
                
else
                    a[j]
=0;
            }
            GetAns();
        }
        printf(
"%d\n",3*ans);
    }
    
return 0;
}
加油啊,加油!

posted on 2012-04-24 15:32 wangs 阅读(344) 评论(0)  编辑 收藏 引用 所属分类: ACM-数据结构


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理