A Rectangular Barn

Mircea Pasoi -- 2003

Ever the capitalist, Farmer John wants to extend his milking business by purchasing more cows. He needs space to build a new barn for the cows.

FJ purchased a rectangular field with R (1 ≤ R ≤ 3,000) rows numbered 1..R and C (1 ≤ C ≤ 3,000) columns numbered 1..C. Unfortunately, he realized too late that some 1x1 areas in the field are damaged, so he cannot build the barn on the entire RxC field.

FJ has counted P (0 ≤ P ≤ 30,000) damaged 1x1 pieces and has asked for your help to find the biggest rectangular barn (i.e., the largest area) that he can build on his land without building on the damaged pieces.

PROGRAM NAME: rectbarn

INPUT FORMAT

  • Line 1: Three space-separated integers: R, C, and P.
  • Lines 2..P+1: Each line contains two space-separated integers, r and c, that give the row and column numbers of a damaged area of the field

SAMPLE INPUT (file rectbarn.in)

3 4 2
1 3
2 1

OUTPUT FORMAT

  • Line 1: The largest possible area of the new barn

SAMPLE OUTPUT (file rectbarn.out)

6

OUTPUT DETAILS

  1 2 3 4
+-+-+-+-+
1| | |X| |
+-+-+-+-+
2|X|#|#|#|
+-+-+-+-+
3| |#|#|#|
+-+-+-+-+

Pieces marked with 'X' are damaged and pieces marked with '#' are part of the new barn.

————————————————————————题解

又见DP……

感受到智商深深的不足

翻译标准题解……

A Rectangular Barn

First, note that the largest possible barn will be touching a rock (i.e., a damaged area) or a side of the field on all four of its sides, as otherwise we could make a larger barn by extending the supposed largest barn on a side which isn't blocked. Consider any rock on the top side. We can view the barn as extending out to the left and to the right from the column of this rock.

首先,注意到最大可能的谷棚会碰到一个岩石(也就是一个损坏区域)或者它四周田地的边界,故而我们可以制造更大的牛棚通过在没有撞上的地方扩展已经确定的最大牛棚。考虑把一个岩石放在顶端,在岩石所在的这一列向左和右扩展。

We loop through the rows, and then through the columns. For the i-th row and the j-th column, we consider the largest barn starting at the most recent rock (or the top side of the field) in the j-th column, and extending down to the i-th row. We have explicitly defined the top and bottom sides of the barn, so we simply want to extend the left and right sides as far out as possible from the j-th column. If we already have the maximum distance from the j-th column that the left and right sides can extend if the bottom side of the barn is the (i-1)-st row, then for each of the left and the right sides, we just take the minimum of the old value and the distance to the nearest rock on the i-th row to calculate the new value for the maximum extensions to the left and the right of our barn. (If we had just hit a rock on the (i-1)-st row, we assume that we could reach all the way to the side of the field.)

我们循环行,然后循环列,在i行j列我们考虑最大的牛棚开始在最近的岩石(或者田地的顶端)在j列,然后扩展到i行。现已明确牛棚上下边界,故而从j列左右拓展,【我就不矫饰语句了】预处理的方法是向左(右)最大延伸距离是对于i-1行的向左(右)延伸最大距离和i行向左(右)延伸最大距离取个max

In this way, we scan down row by row. As each maximal barn is bounded on the top side by some rock or by the top side of the field, we will find it when we're scanning through the row corresponding to the bottom side of the barn.

因为我们一行行扫描【所以我们可以滚动数组,USACO空间只有16M。】因此每个最大的牛棚顶边会限制在一些岩石和田地顶端,我们会找到它当我们扫描这个最大牛棚的底端。

我这里的l[x]记录向左延伸最远到哪个点,r[x]向右延伸最远到哪个点,和题解所叙述不太一样

 /*
ID: ivorysi
LANG: C++
PROG: rectbarn
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <algorithm>
#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 0x5f5f5f5f
#define ivorysi
#define mo 97797977
#define hash 974711
#define base 47
#define fi first
#define se second
#define pii pair<int,int>
#define esp 1e-8
typedef long long ll;
using namespace std;
bool damage[][];
int h[],l[],r[],tl[],tr[];
int n,m,p,x,y,ans;
void solve() {
scanf("%d%d%d",&n,&m,&p);
siji(i,,p) {
scanf("%d%d",&x,&y);
damage[x][y]=;
}
siji(i,,m) {r[i]=m+;l[i]=;}
siji(i,,n) {
tl[]=;tl[m+]=m+;
siji(j,,m) {
if(damage[i][j]) tl[j]=j;
else tl[j]=tl[j-];
}
tr[]=;tr[m+]=m+;
gongzi(j,m,) {
if(damage[i][j]) tr[j]=j;
else tr[j]=tr[j+];
}
siji(j,,m) {
if(damage[i][j]) {
h[j]=;
l[j]=;
r[j]=m+;
}
else {
h[j]=h[j]+;
l[j]=max(l[j],tl[j]+);
r[j]=min(r[j],tr[j]-);
ans=max(ans,(r[j]-l[j]+)*h[j]);
}
}
}
printf("%d\n",ans);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("rectbarn.in","r",stdin);
freopen("rectbarn.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}

USACO 6.1 A Rectangular Barn的更多相关文章

  1. 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 ...

  2. BZOJ1828[USACO 2010 Mar Gold 2.Barn Allocation]——贪心+线段树

    题目描述 输入 第1行:两个用空格隔开的整数:N和M * 第2行到N+1行:第i+1行表示一个整数C_i * 第N+2到N+M+1行: 第i+N+1行表示2个整数 A_i和B_i 输出 * 第一行: ...

  3. USACO Running Away From the Barn /// 可并堆 左偏树维护大顶堆

    题目大意: 给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于m的点有多少个 左偏树 https://blog.csdn.net/pengwill97/article/details/82 ...

  4. USACO 完结的一些感想

    其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...

  5. USACO 6.1 章节

    Postal Vans 题目大意 4*n的网格,要经过所有点的有向有环,不重复经过点的路径总数 n<=1000 题解 显然 插头dp 以4为切面 问题是,会发现 超精度 解决呢要么实现高精度,要 ...

  6. 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 ...

  7. Usaco 1.3.2 修理牛棚(Barn Repair)

      Barn Repair 题意:在一个夜黑风高,下着暴风雨的夜晚,农民约翰的牛棚的屋顶.门被吹飞了. 好在许多牛正在度假,所以牛棚没有住满. 剩下的牛一个紧挨着另一个被排成一行来过夜. 有些牛棚里有 ...

  8. [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 ...

  9. 【USACO 1.3】Barn Repair

    贪心,去掉最大的min(m,c)-1个间隔 /******************************************* TASK: barn1 LANG: C++ Created Tim ...

随机推荐

  1. html5 +css3 点击后水波纹扩散效果 兼容移动端

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  2. OC中线程安全的单例

    @implementation MySingleton + (instancetype)sharedInstance { static MySingleton* instance = nil; sta ...

  3. linux下安装python和pip

    注意:不要轻易去卸载原有的python环境,因为有些软件是依赖他的 一:安装前,先将依赖环境一并安装,避免后面重复编译 [root@redhat2 bin]# yum install gcc g++ ...

  4. codevs 3369 膜拜(线型)

    3369 膜拜 http://codevs.cn/problem/3369/ 题目描述 Description 神牛有很多…当然…每个同学都有自己衷心膜拜的神牛.某学校有两位神牛,神牛甲和神牛乙.新入 ...

  5. 打包python脚本为exe的坎坷经历, by pyinstaller方法

    打包python脚本为exe的坎坷经历, by pyinstaller方法 又应验了那句歌词. 不经历风雨, 怎么见得了彩虹. 安装过程略去不提, 仅提示: pip install pyinstall ...

  6. Spyder简述

    导言 想打造轮子, 就必须要有一套完善的造轮子的工具. 我在jupyter+sciTE的组合里转来转去, 最后还是打算放弃这个组合, 因为离开了自动完成/调用提示/随时随地的访问文档帮助, 前行之路太 ...

  7. soj1036. Crypto Columns

    1036. Crypto Columns Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description The columnar en ...

  8. java学习第01天(程序开发体验)

    1.基本写法 class Demo{ public static void main(String[] args){ System.out.print("Hello World") ...

  9. 【leetcode 简单】 第九十九题 字符串相加

    给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 注意: num1 和num2 的长度都小于 5100. num1 和num2 都只包含数字 0-9. num1 和num2 都不包 ...

  10. python——脚本和print

    脚本和print 1.脚本文件 <Python 基础教程>(第二版)中 P118页,原操作为下: 1 _metaclass_ = type 2 3 class Person: 4 def ...