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个牛所在牛棚的标号 然后求的是如何安排方案,可以使得 ...
随机推荐
- struct和typedef struct区别
分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int a; }Stu; 于是在声明变量的时候就可 ...
- NOIP2012 提高组 Day 1
期望得分:100+100+70=270 实际得分:100+50+70=220 T2 没有底 最后剩余时间来不及打高精.思路出现错误 T1 Vigenère 密码 题目描述 16 世纪法国外交家 Bla ...
- 算法习题-FFT
Q1(hdu1402): 给出两个很大的数字A,B,计算二者乘积. 分析:这个题目java应该能过,用FFT做能够加速计算.这里将字符串A按权(10进制)展开,前面的系数就是多项式的系数,这样就构造出 ...
- pandas 视频讲座 from youtube
Stephen Simmons - Pandas from the inside - YouTube https://www.youtube.com/watch?v=Dr3Hv7aUkmU 2016年 ...
- puppeteer截图
puppeteer是谷歌官方出品的一个通过 DevTools 协议控制 headless Chrome 的Node库.可以通过Puppeteer的提供的api直接控制Chrome模拟大部分用户操作来进 ...
- [译]Quartz.NET 框架 教程(中文版)2.2.x 之第五课 SimpleTrigger
第五课 SimpleTrigger 如果你需要在一个指定时间段内执行一次作业任务或是在指定的时间间隔内多次执行作业任务,SimpleTrigger应该能满足你的调度需求.例如,你希望触发器在2015年 ...
- 对 jQuery 中 data 方法的误解
一直以来都认为新版本中 data 是调用 dataset 实现的,对于低版本IE则采用 getAttribute其实一直是我误解了,也不知道最初这个想法是怎么来的.难道我被盗梦了? 今天 谢亮 兄弟和 ...
- 搭建zookeeper单机版以及简单命令的使用
1:创建目录 #数据目录dataDir=/opt/hadoop/zookeeper-3.3.5-cdh3u5/data#日志目录dataLogDir=/opt/hadoop/zookeeper-3.3 ...
- Struts S2-052漏洞利用之Meterpreter(CVE-2017-9805)
Struts S2-052漏洞爆出来已经快一周了,大家可能更想知道其危害~鸡肋? 这里就直接给出漏洞利用拿Meterpreter的过程,想了解更多的请参考其他文章,下面是实验演示部分.Struts S ...
- datatable表格框架服务器端分页查询设置
更多内容推荐微信公众号,欢迎关注: js代码如下: $('#mytable').dataTable( { "bServerSide": true, //开启服务器模式,使用服务器端 ...