P4417 [COCI2006-2007#2] STOL

题目描述

米尔科买了一套别墅,他想要邀请尽量多的人和他一起庆祝。他需要一张大的木质矩形桌子来让他和他的嘉宾坐下。每张桌子可容纳的人数等于它的周长(四边长度的总和)。米尔科想要买一张即可在他的公寓里放下,也可以坐下尽量多的人和他一起享用晚餐的桌子。桌子必须放置成四条边都与公寓的墙平行的样子。题目给出公寓内部的布局,请问米尔科最多可以邀请多少人吃晚餐。

输入输出格式

输入格式:

第一行包含两个整数R和S(1<=R,S<=400),代表公寓的尺寸(R为宽,S为长),以下的R行中每一行都包含了S个准确的字符(没有空格),代表这个正方形区域是没有东西的('.') ,还是有东西的('X'),米尔科只等把桌子发在没有东西的正方形区域里。

输出格式

输出米尔科在放下他的桌子后能够邀请来吃晚餐的人数

感谢@marcoolmap 提供的翻译

题目描述

Mirko has bought an apartment and wants to invite to dinner as many people as possible to celebrate with him. For this he needs a large rectangular wooden table for which he will sit down with his guests. The number of people a table can accommodate is equal to its perimeter (the sum of the lengths of all four sides). Mirko wants to buy a table such that it fits in his apartment and that as many people as possible can sit down with him for dinner. The table must be placed so that its edges are parallel to the edges of the apartment. Given the layout of the apartment, find the number of people Mirko can invite to dinner.

输入输出格式

输入格式:

The first line contains two integers R and C (1 ≤ R, S ≤ 400), the dimensions of the apartment. Each of the following R rows contains exactly S characters (without spaces), whether a square is free ('.') or blocked ('X'). Mirko can put his table only in free squares.

输出格式:

Output the number of guests Mirko can invite to dinner after he buys his table on a single line.

输入输出样例

输入样例#1: 
2 2
..
..
输出样例#1: 
7
输入样例#2: 
4 4
X.XX
X..X
..X.
..XX
输出样例#2: 
9
输入样例#3: 
3 3
X.X
.X.
X.X
输出样例#3: 
3

 

Solution:

  本题前缀和+暴力枚举。

  首先很容易想到一个超级暴力的思路,处理二维前缀和(表示一个矩形的X个数),再n^4枚举矩形的两个顶点,然后判断矩形内的X个数是否为0,更新答案就好了。

  但是数据$n,m\leq 400$,$O(n^4)$肯定炸了。

  优化一下上面的思路,我们处理每行的前缀和$s[i][j]$表示第$i$行到了$j$列的X个数,更改枚举方法,考虑枚举$l,r$表示矩形的长的左右边界,要使周长最大,就得在此基础上最大化宽度,贪心的从第1行开始判断每行的X个数,记录能连续的最大合法长度就是当前的宽,然后更新周长就好了。

  这样时间复杂度$O(n^3)$,稳妥!

代码:

/*Code by 520 -- 9.5*/
#include
#define il inline
#define ll long long
#define RE register
#define For(i,a,b) for(RE int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(RE int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=505;
int n,m,s[N][N],ans;
char mp[N][N];

int main(){
    scanf("%d%d",&n,&m);
    For(i,1,n) scanf("%s",mp[i]+1);
    For(i,1,n) For(j,1,m) s[i][j]=s[i][j-1]+(mp[i][j]=='X');
    For(l,1,m) For(r,l,m) {
        int ppx=0,lala=0;
        For(k,1,n)
            if(s[k][r]-s[k][l-1]==0){
                if(++ppx>lala) lala=ppx;
            }
            else ppx=0;
        if(lala>0) ans=max(ans,2*lala+2*(r-l+1));
    }
    cout<1;
    return 0;
}

 

转载于:https://www.cnblogs.com/five20/p/9595319.html

你可能感兴趣的:(P4417 [COCI2006-2007#2] STOL)