JZOJ 1237. 餐桌

题目

Description

你家刚买了一套新房,想邀请朋友回来庆祝,所以需要一个很大的举行餐桌,餐桌能容纳的人数等于餐桌的周长,你想买一个能容纳最多人的餐桌,餐桌的边必须跟房间的边平行。
给你的房间的设计,计算最多能邀请的客人数。
 

Input

第一行包含两个整数R和C(1<=R,C<=2000),表示房子的长和宽。
接下来R行每行S个字符(中间没有空格),“.”表示空白区域,“X”表示有障碍物,餐桌所占区域必须是空白的。

Output

输出最多能要求的客人数量。
 

Sample Input

输入1:
2 2
..
..

输入2:
4 4 
X.XX 
X..X 
..X. 
..XX 

输入3:
3 3 
X.X 
.X. 
X.X 

Sample Output

输出1:
7

输出2:
9

输出3:
3
 

Data Constraint

 
 

Hint

【数据规模】
50%的数据R,C<=400
70%的数据R,C<=1000
100%的数据,R,C<=2000

 

分析

 

  • 数据水暴力其实可以过
  • 设up[i][j]为第i行第j格向上找最多的合格
  • n3暴力
  • 或者用单调栈即可

代码

 1 #include<iostream>
 2 using namespace std;
 3 int a[2001][2001],up[2001][2001],st[2001][2001];
 4 int main ()
 5 {
 6     int n,m;
 7     char c;
 8     cin>>n>>m;
 9     for (int i=1;i<=n;i++)
10     {
11         for (int j=1;j<=m;j++)
12         {
13             cin>>c;
14             if (c=='.')
15             {
16                 a[i][j]=1;
17                 up[i][j]=up[i-1][j]+1;
18             }
19             else a[i][j]=0;
20         }
21     }
22     int ans=0;
23     for (int i=1;i<=n;i++)
24     {
25         int top=0,w[55]={0},q[55]={0};
26         for (int j=1,h=0;j<=m+1;j++)
27         {
28             if (j!=m+1) h=up[i][j];
29             else h=0;
30             if (h>q[top])
31                q[++top]=h,w[top]=1;
32             else
33             {
34                 int cnt=0;
35                 while (h<=q[top]&&top>=1)
36                 {
37                     ans=max(ans,2*(w[top]+cnt)+2*q[top]);
38                     cnt+=w[top--];
39                 }
40                 if (j!=m+1&&a[i][j]!=0) q[++top]=h,w[top]=cnt+1;
41             }
42         }
43     }
44     cout<<ans-1;
45 }

猜你喜欢

转载自www.cnblogs.com/zjzjzj/p/11333016.html