USACO 6.1 A Rectangular Barn
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的更多相关文章
- 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 ...
- 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 输出 * 第一行: ...
- USACO Running Away From the Barn /// 可并堆 左偏树维护大顶堆
题目大意: 给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于m的点有多少个 左偏树 https://blog.csdn.net/pengwill97/article/details/82 ...
- USACO 完结的一些感想
其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...
- USACO 6.1 章节
Postal Vans 题目大意 4*n的网格,要经过所有点的有向有环,不重复经过点的路径总数 n<=1000 题解 显然 插头dp 以4为切面 问题是,会发现 超精度 解决呢要么实现高精度,要 ...
- 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 ...
- 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 1.3】Barn Repair
贪心,去掉最大的min(m,c)-1个间隔 /******************************************* TASK: barn1 LANG: C++ Created Tim ...
随机推荐
- Hadoop生态圈-HBase性能优化
Hadoop生态圈-HBase性能优化 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- 转:实现OC与JS的简易交互
oc-->js stringByEvaluatingJavaScriptFromString,其参数是一NSString 字符串内容是js代码(这又可以是一个js函数.一句js代码或他们的组合) ...
- 转:苹果Xcode帮助文档阅读指南
一直想写这么一个东西,长期以来我发现很多初学者的问题在于不掌握学习的方法,所以,Xcode那么好的SDK文档摆在那里,对他们也起不到什么太大的作用.从论坛.微博等等地方看到的初学者提出的问题,也暴露出 ...
- pthread动态库命名规则
Library naming-------------- Because the library is being built using various exceptionhandling sche ...
- Windows下php,mysql,apache相关安装与配置,完善中…
PHP 的安装 由于php是一个zip文件(非install版),安装较为简单解压就行.把解压的 php5.2.1-Win32重命名为 php5.并复制到安装盘目录下.例如安装路径为 c:\php5 ...
- [Baltic2009]Radio Transmission
bzoj 1355: [Baltic2009]Radio Transmission http://www.lydsy.com/JudgeOnline/problem.php?id=1355 Time ...
- 二分算法的应用——最大化平均值 POJ 2976 Dropping tests
最大化平均值 有n个物品的重量和价值分别wi 和 vi.从中选出 k 个物品使得 单位重量 的价值最大. 限制条件: <= k <= n <= ^ <= w_i <= v ...
- Tomcat开启Debug模式
在bin/catalina.sh中添加如下行,将tomcat重启即可. 注:以下标红的7002需将其改成对象的tomcat端口即可! JAVA_OPTS=,server=y,suspend=n -Df ...
- .net WebService 大数据量时性能的提高
1.直接返回DataSet对象 [WebMethod(Description = "直接返回DataSet对象")] public DataSet GetUserListDateS ...
- 【JAVA】配置JAVA环境变量,安装Eclipse
Java程序依赖JDK,就像C#程序依赖.NetFrameWork一样. 所以在开发之前,必须在win7或者是linux上,安装jdk(JavaDevelopkit)里面包括java一些工具,还有JR ...