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. redis sentinel集群

    ip分布情况: sentinel-1/redis 主 10.11.11.5 sentinel-2/redis 从 10.11.11.7 sentinel-3/redis 从 10.11.11.8 ha ...

  2. Android 6.0 7.0 8.0 一个简单的app内更新版本-okgo app版本更新

    登陆时splash初始页调用接口检查app版本.如有更新,使用okGo的文件下载,保存到指定位置,调用Android安装apk. <!-- Android 8.0 (Android O)为了针对 ...

  3. Linux根目录解析

    根目录结构如下: 1. / - 根目录: 每一个文件和目录都从这里开始. 只有root用户具有该目录下的写权限.此目录和/root目录不同,/root目录是root用户的主目录. 2. /bin - ...

  4. Django 2.0.1 官方文档翻译: 高级教程:如何编写可重用的app (page 13)

    高级教程:如何编写可重用的app (page 13) 本节教程上接第七部分(Page 12).我们会把我们的 web-poll应用转换成一个独立的python包,你可以在新的项目中重用或者把它分享给其 ...

  5. 在WindowsServer2008服务器上安装SQLServer2008R2 Express版

    登录服务器 使用远程桌面登录Windows Server 2008   安装前的准备工作 下载SQL Server安装程序 下载Microsoft SQL Server2008 R2 RTM - Ex ...

  6. ASP.NET 数据库缓存依赖

    By Peter A. Bromberg, Ph.D. 在ASP.NET中,Cache类最酷的特点是它能根据各种依赖来良好的控制自己的行为.以文件为基础的依赖是最有用的,文件依赖项是通过使用 Cach ...

  7. caffe设计网络教程(一)

    假设现在我们要设计一个基于VGG的网络,主要考虑的问题是可否修改VGG类似于resnet那样,应该怎么修改?更具体来说,我们需要在VGG网络上考虑eltwise层,现在我们有三种方案,如下: 方案一: ...

  8. Hive笔记之导出查询结果

    一.导出到本地 导出查询结果到本地: INSERT OVERWRITE LOCAL DIRECTORY "/tmp/hive-result/t_visit_video" SELEC ...

  9. Linux服务-配置Nginx反向代理

    任务目标:实现基于轮询的方式调度三台web,并验证结果:实现基于权重的方式调度三台web,并验证结果:实现基于hash的方式调用三台web,并验证结果 由于刚刚做了nfs设置,为了提现实验结果,我在w ...

  10. 【译】第十二篇 SQL Server代理多服务器管理

    本篇文章是SQL Server代理系列的第十二篇,详细内容请参考原文 在这一系列的上一篇,我们查看了维护计划,一个维护计划可能会创建多个作业,多个计划.你还简单地看了SSIS子系统,并查看了维护计划作 ...