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. servlet拦截器

    servlet拦截未登录的用户请求 java代码: package com.gavin.filter; import java.io.IOException; import javax.servlet ...

  2. bzoj千题计划140:bzoj4519: [Cqoi2016]不同的最小割

    http://www.lydsy.com/JudgeOnline/problem.php?id=4519 最小割树 #include<queue> #include<cstdio&g ...

  3. codevs 1540 1540 银河英雄传说

    1540 银河英雄传说 题目描述 Description 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银 ...

  4. Redis学习十一:Redis的Java客户端Jedis

    一.安装JDK tar -zxvf jdk-7u67-linux-i586.tar.gz vi /etc/profile 重启一次Centos 编码验证 二.安装eclipse 三.Jedis所需要的 ...

  5. Linux系统接入小区宽带

    jollywing(jollywing@foxmail.com) 安装 rp-pppoe 今天去联通营业厅开通了家庭宽带,回到家就搜索怎么用Linux接入小区宽带,发现大多数人都选择用PPPOE拨号上 ...

  6. 让老版本IE支持HTML5

    一直想入手C3和H5,但因为所开发的项目一直要求兼容IE7,IE8.而这两个浏览器并不支持html5,所以一直都在观望而未真正的投入太多精力去学习.尽管我知道h5和c3是主流. 在最近的项目开发中,偶 ...

  7. 关于项目中根据当前数据库中最大ID生成下一个ID问题——(五)

    1.关于部门管理时候根据上级产生下级部门ID的问题(传入一个参数是上级部门id)

  8. Java高性能并发编程——线程池

    在通常情况下,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的 ...

  9. sql 内联,左联,右联,全联

    联合查询效率较高,以下例子来说明联合查询(内联.左联.右联.全联)的好处: T1表结构(用户名,密码) userid (int) username varchar(20) password  varc ...

  10. 【驱动】USB驱动实例·串口驱动·键盘驱动【转】

    转自:http://www.cnblogs.com/lcw/p/3159370.html Preface USB体系支持多种类型的设备. 在 Linux内核,所有的USB设备都使用 usb_drive ...