USACO 5.3 Big Barn
Big BarnA 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
————————————————————————————题解
我心里一凉……
saffah刷过USACO?
……这不是重点,然后这道题只要存一个二维前缀和就可以O(1)以i,j为左上角,k为边长的正方形是否合法,然后枚举的话枚举每一个没有树的点,再枚举长度
n^3肯定超时,那么我们第一个优化就是如果当前长度找不到后面也不用扩展了,直接跳出,这样在树比较少图比较大的时候是超时的
第二个优化比较重要,是从左边位置扩展长度-1开始枚举
然后,一二优化加起来,枚举个数不会超过3,因为这个位置要么是左边位置扩展长度-1,要么等于左边位置扩展长度,要么是左边位置扩展长度+1
然后就过了
不过在这里再提一个小想法,二分长度也许也能过
程序看着像一个N^3,实则不然
/*
ID: ivorysi
LANG: C++
PROG: bigbrn
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#include <cmath>
#include <stack>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x3f3f3f3f
#define ivorysi
#define mo 97797977
#define hash 974711
#define base 47
#define pss pair<string,string>
#define MAXN 5000
#define fi first
#define se second
#define pii pair<int,int>
#define esp 1e-8
typedef long long ll;
using namespace std;
int n,m;
int a[][],sum[][],sol[][],ans;
bool check(int x,int y,int k) {
return (sum[x+k-][y+k-]+sum[x-][y-]-sum[x-][y+k-]-sum[x+k-][y-])==;
}
void solve() {
scanf("%d%d",&n,&m);
siji(i,,n+) {a[i][]=;a[i][n+]=;a[][i]=;a[n+][i]=;}//边上种上一圈树
int x,y,t;
siji(i,,m) {
scanf("%d%d",&x,&y);
a[x][y]=;
}
siji(i,,n+) sum[i][]=a[i][];
siji(i,,n+) { siji(j,,n+) {
sum[i][j]=sum[i][j-]+a[i][j];
}
}
siji(i,,n+) {
siji(j,,n+) {
sum[i][j]+=sum[i-][j];
}
}
siji(i,,n) {
siji(j,,n) {
if(a[i][j]==) continue;
t=max(sol[i][j-]-,);
siji(k,t,n) {
if(!check(i,j,k)) break;
sol[i][j]=k;
}
ans=max(ans,sol[i][j]);
}
}
printf("%d\n",ans);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("bigbrn.in","r",stdin);
freopen("bigbrn.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}
USACO 5.3 Big Barn的更多相关文章
- 【USACO 1.3】Barn Repair
贪心,去掉最大的min(m,c)-1个间隔 /******************************************* TASK: barn1 LANG: C++ Created Tim ...
- USACO Section 1.3 Barn Repair 解题报告
题目 题目描述 某农夫有一个养牛场,所有的牛圈都相邻的排成一排(共有S个牛圈),每个牛圈里面最多只圈养一头牛.有一天狂风卷积着乌云,电闪雷鸣,把牛圈的门给刮走了.幸运的是,有些牛因为放假,所以没在自己 ...
- USACO 1.3.2 Barn Repair 修理牛棚(贪心)
Description 在一个夜黑风高,下着暴风雨的夜晚,农民约翰的牛棚的屋顶.门被吹飞了. 好在许多牛正在度假,所以牛棚没有住满. 剩下的牛一个紧挨着另一个被排成一行来过夜. 有些牛棚里有牛,有些没 ...
- USACO 完结的一些感想
其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...
- USACO Section 5.3 Big Barn(dp)
USACO前面好像有类似的题目..dp(i,j)=min(dp(i+1,j),dp(i+1,j+1),dp(i,j+1))+1 (坐标(i,j)处无tree;有tree自然dp(i,j)=0) .d ...
- USACO 6.1 A Rectangular Barn
A Rectangular Barn Mircea Pasoi -- 2003 Ever the capitalist, Farmer John wants to extend his milking ...
- Usaco 1.3.2 修理牛棚(Barn Repair)
Barn Repair 题意:在一个夜黑风高,下着暴风雨的夜晚,农民约翰的牛棚的屋顶.门被吹飞了. 好在许多牛正在度假,所以牛棚没有住满. 剩下的牛一个紧挨着另一个被排成一行来过夜. 有些牛棚里有 ...
- [USACO 12DEC]Running Away From the Barn
Description It's milking time at Farmer John's farm, but the cows have all run away! Farmer John nee ...
- USACO Barn Repair 【贪心算法】
这到题目的题意不太好理解= = 看来还是英语太弱了 实际上题目给了你M, S, C 分别代表最多不超过M 块木板, S代表牛棚总数,C代表接下来有C个牛所在牛棚的标号 然后求的是如何安排方案,可以使得 ...
随机推荐
- python 启航
first = 1while first<=9: sec = 1 while sec <= first: print( str(sec)+"*&qu ...
- jquery validate 增加过滤特殊字符的方法
jQuery.validator.addMethod("specialCharFilter", function(value, element) { var pattern = n ...
- 互斥量、条件变量与pthread_cond_wait()函数的使用,详解(一)
1. 首先pthread_cond_wait 的定义是这样的 The pthread_cond_wait() and pthread_cond_timedwait() functions are us ...
- 高性能流媒体服务器EasyDarwin
标准RTSP拉模式直播(EasyRelayModule):适合内部监控 分布式部署(EasyCMSModule):负载均衡主要是用Reids作为负载
- PyQt4 里的表格部件的使用方法: QTableWidget
PyQt4 里的表格部件的使用方法: QTableWidget QT下QTableWidget使用方法小结 - - 博客频道 - CSDN.NET http://blog.csdn.net/jingz ...
- [洛谷P1228]地毯填补问题 题解(分治)
Description 相传在一个古老的阿拉伯国家里,有一座宫殿.宫殿里有个四四方方的格子迷宫,国王选择驸马的方法非常特殊,也非常简单:公主就站在其中一个方格子上,只要谁能用地毯将除公主站立的地方外的 ...
- UNIX环境高级编程 第8章 进程控制
本章是UNIX系统中进程控制原语,包括进程创建.执行新程序.进程终止,另外还会对进程的属性加以说明,包括进程ID.实际/有效用户ID. 进程标识 每个进程某一时刻在系统中都是独一无二的,它们之间是用一 ...
- 2016.5.16——leetcode:Reverse Bits(超详细讲解)
leetcode:Reverse Bits 本题目收获 移位(<< >>), 或(|),与(&)计算的妙用 题目: Reverse bits of a given 3 ...
- sqlplus设置长度
1.set linesize 100 2.col XX format a30 3.col XXX format 9,999,999,999 3.set heading off 表头不显示
- Office DDE漏洞学习笔记
1.前言 2017年下半年爆发出来的Office漏洞,一直没有空做笔记记录.在病毒分析中也看到有利用这个漏洞的样本,针对Office系列软件发起的钓鱼攻击和APT攻击一直是安全攻防的热点. 2.off ...