题目背景

(USACO 5.3.4)

题目描述

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

EXAMPLE

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

1 2 3 4 5 6 7 8

1 . . . . . . . .

2 . # . . . # . .

3 . . . . . . . .

4 . . . . . . . .

5 . . . . . . . .

6 . . # . . . . .

7 . . . . . . . .

8 . . . . . . . .

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

输入输出格式

输入格式:

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

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

输出格式:

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

输入输出样例

输入样例#1:

8 3
2 2
2 6
6 3
输出样例#1:

5

说明

题目翻译来自NOCOW。

USACO Training Section 5.3

题目大意:求最大子正方形的边长。

题解:

O(n^3)暴力竟然过了....

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,t,sum[][];
int main(){
scanf("%d%d",&n,&t);
for(int i=;i<=t;i++){
int x,y;
scanf("%d%d",&x,&y);
sum[x][y]=;
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
sum[i][j]+=sum[i-][j]+sum[i][j-]-sum[i-][j-];
for( int len=n;len>=;len--){
for( int i=;i<=n-len+;i++){
for(register int j=;j<=n-len+;j++){
int x=i+len-,y=j+len-;
if(sum[x][y]-sum[i-][y]-sum[x][j-]+sum[i-][j-]==){
printf("%d\n",len);
return ;
}
}
}
}
return ;
}

dp

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,t,x,y,ans,map[][],f[][];
int main(){
scanf("%d%d",&n,&t);
for(int i=;i<=t;i++){
scanf("%d%d",&x,&y);
map[x][y]=;
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
if(map[i][j]) continue;
f[i][j]=min(f[i-][j],min(f[i-][j-],f[i][j-]))+;
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
ans=max(ans,f[i][j]);
cout<<ans;
}

最新文章

  1. iOS---数据本地化
  2. [terry笔记]ora-00904 invalid identifier—同义词
  3. POJ3525 Most Distant Point from the Sea(半平面交)
  4. Delphi开发OCX详细步骤总结
  5. 简单版解决IE兼容性问题
  6. ASP.NET 运行机制续(完结)
  7. js中的String数据类型
  8. TCP三次握手和http过程
  9. xcode 工具 alcatraz---备用
  10. Python之路,Day15 - Django适当进阶篇
  11. machine learn in python 第二章2.1.1
  12. Java中泛型 类型擦除
  13. html/css/javascript的含义、作用及理解
  14. mysql5.6源码自动安装脚本
  15. android binder机制详解
  16. Java_比较两个图片的相似度
  17. 基于Verilog的带FIFO输出缓冲的串口接收接口封装
  18. MySQL用户远程登录问题
  19. 第19月第2天 cellForItemAtIndexPath 返回空
  20. spring-service.xml 模板

热门文章

  1. 【BZOJ4009】[HNOI2015]接水果 DFS序+整体二分+扫描线+树状数组
  2. HTML-Table-Td固定宽度使内容换行
  3. Linux安装samba
  4. bug_1——oracle listagg():列转行
  5. tf.InteractiveSession()与tf.Session()
  6. 解决ajax get方式提交中文参数乱码问题
  7. spark0.9.0安装
  8. pycharm ctrl+滚轮调节字体大小
  9. css3图片过滤效果
  10. render总结