bzoj4578: [Usaco2016 OPen]Splitting the Field

bzoj4578: [Usaco2016 OPen]Splitting the Field

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 56  Solved: 15
[Submit][Status][Discuss]

Description

Farmer John‘s N cows (3≤N≤50,000) are all located at distinct positions in his two-dimensional fie

ld. FJ wants to enclose all of the cows with a rectangular fence whose sides are parallel to the x a

nd y axes, and he wants this fence to be as small as possible so that it contains everycow (cows on

the boundary are allowed).FJ is unfortunately on a tight budget due to low milk production last quar

ter. 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 muc

h 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 allow

ed), and they must have sides parallel to the x and y axes. The two enclosures are notallowed to ove

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

Input

The first line of input contains N. The nextNN lines each contain two integers specifying the location of a cow. Cow locations are positive integers in the range 1…1,000,000,000

Output

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

f one.

Sample Input

6
4 2
8 10
1 1
9 12
14 7
2 3

Sample Output

107

HINT

Source

题意:给出若干个点 问如果用两个矩形覆盖所有点会比用一个矩形覆盖所有点节省多少面积

题解:。。。好坑啊。。。矩形在边界上似乎是可以相交的然而题目说不行。。。然后面积应该是矩形的 (x2-x1) * (y2-y1) ..是不用加一的,。。

首先对所有点排序。。然后我们可以考虑枚举两个矩形的分割线。。又因为分界线可以是横的也可以是竖的...所以要枚举两次

具体可以看代码

#include<algorithm>
#include<cstdio>
#include<iostream>
#include<cstring>
#define N 50015
#define down(i,r,l) for(int i=r;i>=l;i--)
#define rep(i,l,r) for(int i=l;i<=r;i++)
using namespace std;
typedef long long ll;
int l[N],r[N],l1[N],r1[N],n;
ll zs,ans;
struct node{int x,y;}s[N];
bool cmp(node x,node y) {return x.x==y.x?x.y<y.y:x.x<y.x;}
void getans() {
     ll now;
     sort(s+1,s+1+n,cmp);
     memset(l,60,(n+10)<<2); memset(l1,60,(n+10)<<2); memset(r,0,(n+10)<<2); memset(r1,0,(n+10)<<2);
     rep(i,1,n) l[i]=min(l[i-1],s[i].y),r[i]=max(r[i-1],s[i].y);
     down(i,n,1) l1[i]=min(l1[i+1],s[i].y),r1[i]=max(r1[i+1],s[i].y);
     rep(i,2,n) {
          now=(ll)(s[i-1].x-s[1].x) * (ll)(r[i-1]-l[i-1]) + (ll)(s[n].x-s[i].x) * (ll)(r1[i]-l1[i]);
          ans=min(ans,now);
     }
}
int main () {
     int miny,minx,maxx,maxy;
     miny=minx=1000000050,maxy=maxx=0;
     cin>>n; rep(i,1,n) {
         scanf("%d%d",&s[i].x,&s[i].y),miny=min(miny,s[i].y),maxy=max(maxy,s[i].y);;
         maxx=max(maxx,s[i].x); minx=min(minx,s[i].x);
     }
     ans=(ll)(maxy-miny)*(ll)(maxx-minx),zs=ans;
     getans();
     rep(i,1,n) swap(s[i].x,s[i].y);
     getans();
     printf("%lld\n",zs-ans);
}

时间: 2024-08-29 17:20:38

bzoj4578: [Usaco2016 OPen]Splitting the Field的相关文章

洛谷P3145 [USACO16OPEN]分割田地Splitting the Field

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 pa

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 thi

BZOJ 4581: [Usaco2016 Open]Field Reduction

Description 有 \(n\) 个点,删掉三个点后,求最小能围住的面积. Sol 搜索. 找出 左边/右边/上边/下边 的几个点枚举就可以了. 我找了 12 个点,统计一下坐标的个数,然后找到最大最小坐标. PS:md.Cena 好像对 STL 不太友好啊,用 map 居然T了???exm??? 昨天用 map 离散化就T的飞起.md..mama我再也不用 map 离散化了...以后用lower_bound()好了... 一开始写了个DFS来枚举...发现不是很好...又删掉变成三维循环

redmine computed custom field formula tips

项目中要用到Computed custom field插件,公式不知道怎么写,查了些资料,记录在这里. 1.http://apidock.com/ruby/Time/strftime 查看ruby的字符串格式,用于改写Date/time format只显示日期,不显示时间. 2.https://github.com/annikoff/redmine_plugin_computed_custom_field/issues/34 看到formula里可以写复杂代码,比如增加变量.指定返回值等. 3.

lucene中Field.Index,Field.Store的一些设置

lucene在doc.add(new Field("content",curArt.getContent(),Field.Store.NO,Field.Index.TOKENIZED)); Field有两个属性可选:存储和索引. 通过存储属性你可以控制是否对这个Field进行存储: 通过索引属性你可以控制是否对该Field进行索引. 事实上对这两个属性的正确组合很重要. Field.Index Field.Store 说明 TOKENIZED(分词) YES 被分词索引且存储 TOKE

错误号:1364 错误信息:Field &#39;platId&#39; doesn&#39;t have a default value

1. 错误描述 错误号:1364 错误信息:Field 'platId' doesn't have a default value insert into `use`.`t_platform_scale` () values () 2.错误原因 由于在建表时,给platId一个not null限制,而且没有给默认值,在插入数据时未插入,导致报错 3.解决办法 (1)修改platId可以为null (2)插入数据时一定要插入platId 版权声明:本文为博主原创文章,未经博主允许不得转载. 错误号

【java】java反射机制,动态获取对象的属性和对应的参数值,并属性按照字典序排序,Field.setAccessible()方法的说明【可用于微信支付 签名生成】

方法1:通过get()方法获取属性值 package com.sxd.test.controller; public class FirstCa{ private Integer num; private String name; private Boolean flag; public Integer getNum() { return num; } public void setNum(Integer num) { this.num = num; } public String getNam

【反射】Class Field Method Constructor

Class public final class java.lang.Class<T> extends Object implements Serializable, GenericDeclaration, Type, AnnotatedElement 类型参数 T:由此 Class 对象建模的类的类型.例如, String.class 的类型是 Class<String>.如果将被建模的类未知,则使用 Class<?>. Instances of the class

【反射之Field】获取字段

■getFields().getDeclaredFields().getField() 和 getDeclaredField()的用法 1 package reflect.field; 2 3 import java.lang.reflect.Constructor; 4 import java.lang.reflect.Field; 5 import java.lang.reflect.InvocationTargetException; 6 import java.lang.reflect.