P3145 [USACO16OPEN]分割田地Splitting the Field

题目描述

Farmer John's NN cows (3 \leq N \leq 50,0003≤N≤50,000) are all located at distinct positions in his two-dimensional field. FJ wants to enclose all of the cows with a rectangular fence whose sides are parallel to the x and y axes, and hewants this fence to be as small as possible so that it contains every cow (cowson the boundary are allowed).

FJ is unfortunately on a tight budget due to low milk production last quarter.He would therefore like to enclose a smaller area to reduce maintenance costs,and the only way he can see to do this is by building two enclosures instead of one. Please help him compute how much less area he needs to enclose, in total,by using two enclosures instead of one. Like the original enclosure, the two enclosures must collectively contain all the cows (with cows on boundaries allowed), and they must have sides parallel to the x and y axes. The two enclosures are not allowed to overlap -- not even on their boundaries. Note that enclosures of zero area are legal, for example if an enclosure has zero width and/or zero height.在一个二维的牧场中,Farmer John的N(3<=N<=50000)头牛都各占一席。他想用边平行于x轴和y轴的矩形围栏围住所有牛,并且要让围栏尽可能小(牛可以在边界线上)。

不幸地,由于Farmer John的奶牛产量惨淡,导致最后一个季度预算紧张。因此,他希望封闭一个较小的地区来减少维修的费用,他能看到的唯一方法就是修建两个围栏而不是建一个。请编程告诉他用两个围栏比用一个围栏总共能够节省多少需要围住的面积。同样地,用两个围栏的时候必须围住所有的牛(牛同样可以在边界上),边也要平行于x轴和y轴。两个围栏不允许重叠(边界也不能)。注意面积为零是合法的,例如一个围栏有着长度为零的宽或长度为零的长(一条线)。

输入输出格式

输入格式:

The first line of input contains NN. The next NN lines each contain two

integers specifying the location of a cow. Cow locations are positive integers

in the range 1 \ldots 1,000,000,0001…1,000,000,000.

输出格式:

Write a single integer specifying amount of total area FJ can save by using two

enclosures instead of one.

输入输出样例

输入样例#1: 复制

6
4 2
8 10
1 1
9 12
14 7
2 3
输出样例#1: 复制

107
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 50010
#define INF 0x7fffffff
int l[maxn],r[maxn],l1[maxn],r1[maxn],n;
long long ans;
struct node{
int x,y;
bool operator < (const node a)const{
if(x==a.x)return y<a.y;
return x<a.x;
}
}p[maxn];
void work(){
memset(l,,sizeof(l));
memset(r,-,sizeof(r));
memset(l1,,sizeof(l1));
memset(r1,-,sizeof(r1));
sort(p+,p+n+);
for(int i=;i<=n;i++){
l[i]=min(l[i-],p[i].y);
r[i]=max(r[i-],p[i].y);
}
for(int i=n;i>=;i--){
l1[i]=min(l1[i+],p[i].y);
r1[i]=max(r1[i+],p[i].y);
}
for(int i=;i<=n;i++){
long long tem=1LL*(r[i-]-l[i-])*(p[i-].x-p[].x)+1LL*(r1[i]-l1[i])*(p[n].x-p[i].x);
ans=min(tem,ans);
}
}
int main(){
int mnx=INF,mny=INF,mxx=-INF,mxy=-INF;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d",&p[i].x,&p[i].y);
mnx=min(mnx,p[i].x);mny=min(mny,p[i].y);
mxx=max(mxx,p[i].x);mxy=max(mxy,p[i].y);
}
ans=1LL*(mxx-mnx)*(mxy-mny);
long long ans1=ans;
work();
for(int i=;i<=n;i++)swap(p[i].x,p[i].y);
work();
cout<<ans1-ans;
return ;
}

洛谷P3145 [USACO16OPEN]分割田地Splitting the Field的更多相关文章

  1. 洛谷P1436 棋盘分割

    洛谷题目链接 动态规划: 我们设状态$f[i][j][o][p][k]$表示一个矩形,左上角顶点坐标为$(i,j)$,右下角顶点坐标为$(o,p)$时分割了$k$次,也就是说现在是$k+1$块 我们考 ...

  2. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告

    P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...

  3. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解

    P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...

  4. 洛谷 P1436 棋盘分割 解题报告

    P1436 棋盘分割 题目描述 将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的两部分中的任意一块继续如此分割,这样割了(n-1)次后,连同最后剩下的矩形棋盘共 ...

  5. 洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver

    题目描述 Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to tem ...

  6. 洛谷 P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver

    传送门 题目大意: n个谷仓 ,每次关闭一个谷仓,问剩下没被关闭的谷仓是 否联通. 题解:并查集+倒序处理 代码: #include<iostream> #include<cstdi ...

  7. 【前缀和】【two-pointer】【贪心】洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解

        解法众多的一道毒瘤题? 题目描述 奶牛Bessie很喜欢闪亮亮的东西(Baling~Baling~),所以她喜欢在她的空余时间开采钻石!她现在已经收集了\(N\)颗不同大小的钻石,现在她想在谷 ...

  8. 洛谷P3147 [USACO16OPEN]262144

    P3147 [USACO16OPEN]262144 题目描述 Bessie likes downloading games to play on her cell phone, even though ...

  9. 洛谷P3146 [USACO16OPEN]248

    P3146 [USACO16OPEN]248 题目描述 Bessie likes downloading games to play on her cell phone, even though sh ...

随机推荐

  1. python-字典和json

    Python的字典和JSON在表现形式上非常相似 #这是Python中的一个字典 dic = { 'str': 'this is a string', 'list': [1, 2, 'a', 'b'] ...

  2. linux命令学习笔记(20):find命令之exec

    find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作, 这个时候exec的作用就显现出来了. exec解释: -exec 参数后面跟的是command ...

  3. OpenCV - Android Studio 中集成Opencv环境(包含opencv_contrib部分)

    我在上一篇博客中说到了在Android中集成OpenCV,但是那个版本的OpenCV是没有SIFT和SURF算法的,因为这些算法是受专利保护的,所以并没有被包含在预编译库中,所以如果想要使用SIFT和 ...

  4. freeMarker(四)——模板开发指南之模板

    学习笔记,选自freeMarker中文文档,译自 Email: ddekany at users.sourceforge.net 模板开发指南之模板 1. 总体结构 实际上用程序语言编写的程序就是模板 ...

  5. NOI 模拟赛 #2

    得分非常惨惨,半个小时写的纯暴力 70 分竟然拿了 rank 1... 如果 OYJason 和 wxjor 在可能会被爆踩吧 嘤 T1 欧拉子图 给一个无向图,如果一个边集的导出子图是一个欧拉回路, ...

  6. MySQL_西安11月销售昨日未上架的产品_20161212

    #C034西安11月销售昨日未上架的产品  SELECT 城市,a.订单日期,a.客户数,a.订单数,b.产品数,a.金额,c.销售确认额,c.毛利额,c.毛利率 FROM ( SELECT 城市,订 ...

  7. Poj 2403 Hay Points(Map)

    一.题目大意 实现一个工资计算系统.工资的计算规则是:首先,给定一些关键字和对应的价值,这个相对于字典.然后给出的是求职者的描述,如果这个描述中包含关键字则加上对应的价值,总得价值就是这个求职者的工资 ...

  8. mongodb 的命令操作(转)

    成功启动MongoDB后,再打开一个命令行窗口输入mongo,就可以进行数据库的一些操作. 输入help可以看到基本操作命令: show dbs:显示数据库列表  show collections:显 ...

  9. 关于使用C# 启动msi失败的问题

    原以为在启动msi是件小儿科的事,上代码: ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = "C:\\myTest ...

  10. PHP二维数组,根据多个字段来排序

    如果是最最常见的二维数组排序, 大多数情况下也只用到二维: 用php内置函数 array_multisort( )  是最简单的: <?php 假设, $arr 是一个二维数组, $arg1是取 ...