usaco training 5.3.4 Big Barn 题解

【原题】

Big Barn
A Special Treat

Farmer John wants to place a big square barn on his square farm. He hates to cut down trees on his farm and wants to find a location for his barn that enables him to build it only on land that is already clear of trees. For our purposes, his land is divided into N x N parcels. The input contains a list of parcels that contain trees. Your job is to determine and report the largest possible square barn that can be placed on his land without having to clear away trees. The barn sides must be parallel to the horizontal or vertical axis.

EXAMPLE

Consider the following grid of Farmer John's land where `.' represents a parcel with no trees and `#' represents a parcel with trees:

          1 2 3 4 5 6 7 8
        1 . . . . . . . .
        2 . # . . . # . .
        3 . . . . . . . .
        4 . . . . . . . .
        5 . . . . . . . .
        6 . . # . . . . .
        7 . . . . . . . .
        8 . . . . . . . .

The largest barn is 5 x 5 and can be placed in either of two locations in the lower right part of the grid.

PROGRAM NAME: bigbrn

INPUT FORMAT

Line 1:Two integers: N (1 <= N <= 1000), the number of parcels on a side, and T (1 <= T <= 10,000) the number of parcels with trees
Lines 2..T+1:Two integers (1 <= each integer <= N), the row and column of a tree parcel

SAMPLE INPUT (file bigbrn.in)

8 3
2 2
2 6
6 3

OUTPUT FORMAT

The output file should consist of exactly one line, the maximum side length of John's barn.

SAMPLE OUTPUT (file bigbrn.out)

5

【议题】

描述

农夫约翰想要在他的正方形农场上建造一座正方形大牛棚。他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方。我们假定,他的农场划分成 N x N 的方格。输入数据中包括有树的方格的列表。你的任务是计算并输出,在他的农场中,不需要砍树却能够修建的最大正方形牛棚。牛棚的边必须和水平轴或者垂直轴平行。

[编辑]格式

EXAMPLE

考虑下面的方格,它表示农夫约翰的农场,‘.'表示没有树的方格,‘#'表示有树的方格

         1 2 3 4 5 6 7 8
       1 . . . . . . . .
       2 . # . . . # . .
       3 . . . . . . . .
       4 . . . . . . . .
       5 . . . . . . . .
       6 . . # . . . . .
       7 . . . . . . . .
       8 . . . . . . . .

最大的牛棚是 5 x 5 的,可以建造在方格右下角的两个位置其中一个。

PROGRAM NAME: bigbrn

INPUT FORMAT

Line 1: 两个整数: N (1 <= N <= 1000),农场的大小,和 T (1 <= T <= 10,000)有树的方格的数量

Lines 2..T+1: 两个整数(1 <= 整数 <= N), 有树格子的横纵坐标

OUTPUT FORMAT

输出文件只由一行组成,约翰的牛棚的最大边长。

[编辑]SAMPLE INPUT (file bigbrn.in)

8 3
2 2
2 6
6 3

[编辑]SAMPLE OUTPUT (file bigbrn.out)

5


【分析】这就是经典的最大子正方形的题目,n^2足以。上次做单调队列的时候,连最大子长方形都能够用n^2求出。但是USACO的坑点就是内存只有16M.按照标准的思路是不能成功的。于是又要开滚存了。麻烦。

【代码】

/*
PROG:bigbrn
ID:juan1973
LANG:C++
*/
#include<stdio.h>
#include<iostream>
#include<cstring>
using namespace std;
const long maxn=1001;
long f[2][maxn],lef[maxn][maxn],up[maxn][maxn],a[maxn][maxn],n,m,i,j,ans,x,y,now;
int main()
{
  freopen("bigbrn.in","r",stdin);
  freopen("bigbrn.out","w",stdout);
  scanf("%ld%ld",&n,&m);
  for (i=1;i<=m;i++)
  {
    scanf("%ld%ld",&x,&y);
    a[x][y]=1;
  }
  for (i=1;i<=n;i++)
  {
    for (j=1;j<=n;j++)
      if (a[i][j]==0) lef[i][j]=lef[i][j-1]+1;else lef[i][j]=0;
  }
  for (j=1;j<=n;j++)
  {
    for (i=1;i<=n;i++)
      if (a[i][j]==0) up[i][j]=up[i-1][j]+1;else up[i][j]=0;
  }
  now=0;
  for (i=1;i<=n;i++)
  {
    now^=1;
    memset(f[now],0,sizeof(f[now]));
    for (j=1;j<=n;j++)
      if (a[i][j]==0) 
        {
          f[now][j]=min(min(up[i][j],lef[i][j]),f[now^1][j-1]+1);
          ans=max(ans,f[now][j]);
        }
  }
  printf("%ld\n",ans);
  return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值