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); }