[usaco]5.4.5 Big Barn题解

5
 
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

 

--------------------------------------------------------------------------------

以前做过类似的题,所以这次特别的快。
算法很简单,记录每一个节点向左上方能够延伸出来的最大的方块的大小,以及能够向左延伸的最大距离,还有向上延伸的最大距离。
这样一来,如果某个节点是树木,那么这个节点的2个变量置0,否则left和up分别是上一个节点累加1,而squre取左上方一个节点+1,和left还有up相比较,取最小值,就是当前节点的squre。

还有个节省空间的办法,left,up还有squre三个矩阵并不需要n×n,因为有些行用过之后就没有用了,因此只需要保存两行就行了。

USER: Ma yunlei [yunleis2]
TASK: bigbrn
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 4024 KB]
   Test 2: TEST OK [0.000 secs, 4024 KB]
   Test 3: TEST OK [0.000 secs, 4024 KB]
   Test 4: TEST OK [0.000 secs, 4024 KB]
   Test 5: TEST OK [0.000 secs, 4024 KB]
   Test 6: TEST OK [0.000 secs, 4024 KB]
   Test 7: TEST OK [0.000 secs, 4024 KB]
   Test 8: TEST OK [0.000 secs, 4024 KB]
   Test 9: TEST OK [0.000 secs, 4024 KB]
   Test 10: TEST OK [0.027 secs, 4024 KB]
   Test 11: TEST OK [0.027 secs, 4024 KB]
   Test 12: TEST OK [0.027 secs, 4024 KB]
   Test 13: TEST OK [0.027 secs, 4024 KB]
   Test 14: TEST OK [0.027 secs, 4024 KB]
   Test 15: TEST OK [0.027 secs, 4024 KB]

All tests OK.
YOUR PROGRAM ('bigbrn') WORKED FIRST TIME!  That's fantastic
-- and a rare thing.  Please accept these special automated
congratulations.

---------------------------------------------------------------------------

/*
ID:yunleis3
PROG:bigbrn
LANG:C++
*/

#include <fstream>
#include<iostream>

using namespace std;
const int maxn=1001;
const int maxt=10001;
bool metri[maxn][maxn];
int up[2][maxn];
int left1[2][maxn];
int squr[2][maxn];
int n,t;
int large=0;
#define rowindex(row)  ((row)%2)
#define min(x,y)   (x<y?x:y)
#define trimin(x,y,z)  (min(min(x,y),z))
int main()
{
	ifstream fin("bigbrn.in");
	fin>>n>>t;
	for(int i=0;i<t;++i){
		int a,b;
		fin>>a>>b;
		metri[a][b]=true;
	}
	for(int row=1;row<=n;++row){
		for(int col=1;col<=n;++col){
			if(metri[row][col]){
				up[rowindex(row)][col]=0;
				left1[rowindex(row)][col]=0;
				squr[rowindex(row)][col]=0;
			}else {
				up[rowindex(row)][col]=up[rowindex(row-1)][col]+1;
				left1[rowindex(row)][col]=left1[rowindex(row)][col-1]+1;
				squr[rowindex(row)][col]=trimin(up[rowindex(row)][col],left1[rowindex(row)][col],squr[rowindex(row-1)][col-1]+1);
				if(large<squr[rowindex(row)][col])
					large=squr[rowindex(row)][col];
			}
		}
	}
	ofstream fout("bigbrn.out");
	fout<<large<<endl;
	//system("pause");
}

 

时间: 2024-10-26 03:14:43

[usaco]5.4.5 Big Barn题解的相关文章

2007淘宝UED招聘题解(前端开发部分)

史上最酷的招聘已经结束,效果还是不错的.我们发现了很多颇有潜力的人才.他们的一些聘题的解法也开拓了我们的视野,让我们收获良多.感谢所有参与招聘及所有关注淘宝UED博客的朋友. 以下是该次招聘前端开发工程师的聘题解答: 小贤是一条可爱的小狗(Dog),它的叫声很好听(wow),每次看到主人的时候就会乖乖叫一声(yelp). 从这段描述可以得到以下对象: function Dog() { this.wow = function() { alert('Wow'); } this.yelp = func

Codeforces B. Taxi 算法题解

简单总结题意: 有一个数组,数组中的数值不会大于4,即只能是1,2,3,4,要把这些数值装进一个容量最大为4的容器里面,使得所用到这样的容器的个数最小. 经测试数据很大,会有10万个数据,所以这里我并不用排序数据,而是使用counting sort的思想,根据特定的数据优化,使得题解时间复杂度为O(n). 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/ 程序如下: #include <iostream>

code chef:Counting Matrices题解

题目:给定一个数值,找出2*2矩阵的对角和等于N(N<2500)和行列式大于0的个数. 这里是考数学知识了,程序不难,不过要精确计算好却也不容易. 最原始的程序,但是会超时: void CountingMatrices() { int T = 0, N = 0; cin>>T; while (T--) { cin>>N; long long ans = 0; for (int i = 1; i < N; i++) { int a = i, b = N-i; for (i

code chef:Cool Guys题解

All submissions for this problem are available. Given an integer N. Integers A and B are chosen randomly in the range [1..N]. Calculate the probability that the Greatest Common Divisor(GCD) of A and B equals to B. Input The first line of the input co

usaco的一道题:packrec是暴搜求第六种方案思路

问题描述 usaco的一道题:packrec是暴搜求第六种方案思路 usaco上有一道我觉得对于我来说很变态的暴搜题,翻译过来是这样的 给定4个矩形块找出一个最小的封闭矩形将这4个矩形块放入但不得相互重叠.所谓最小矩形指该矩形面积最小. 所有4个矩形块的边都与封闭矩形的边相平行图1示出了铺放4个矩形块的6种方案.这6种方案仅只是可能的基本铺放方案.因为其它方案能由基本方案通过旋转和镜像反射得到. 可能存在满足条件且有着同样面积的各种不同的封闭矩形你应该输出所有这些封闭矩形的边长. INPUT F

c-C 在做USACO Greedy Gift Givers的时候遇到bus error 10

问题描述 C 在做USACO Greedy Gift Givers的时候遇到bus error 10 下面是我的代码 (C语言) #include <stdio.h> #include <stdlib.h> #include <string.h> FILE *input_file, *output_file; int total_people, giving_to_counter, accepting, having[9]; char giver[9][13], rec

[usaco]4.2.2偶图匹配 The Perfect Stall

The Perfect Stall Hal Burch Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John

Android L 值不值得刷?十个问题解疑惑

笔者今天把大家对Android L预览版的问题进行一个汇总,并挑选十个最受用户关注的问题进行一一回答.相信当你看完这十个问题后,Android L预览版到底值不值得刷?你心中一定会有自己的答案. (一)Android L预览版是什么? 在今年6月26日的谷歌在I/O开发者大会上,谷歌正式推出Android L.虽然没有等到Android 5.0的实际,但Android L的出现,也可以说是Android系统自2008年问世以来变化最大的升级.除了新的用户界面.性能升级和跨平台支持,全面的电池寿命

[usaco]超级素数 superprime

偶也,纪念一次成功,首先发测试结果 USER: Ma yunlei [yunleis2]TASK: sprimeLANG: C++ Compiling...Compile: OK Executing...   Test 1: TEST OK [0.000 secs, 3028 KB]   Test 2: TEST OK [0.000 secs, 3028 KB]   Test 3: TEST OK [0.000 secs, 3028 KB]   Test 4: TEST OK [0.000 se