bzoj4578: [Usaco2016 open]splitting the Field time limit:10 Sec Memory limit:128 MB
submit:56 solved:15
[Submit] [Status] [Discuss] Descriptionfarmer John ' s N cows (3≤n≤50,000) is all located at distinct positions in his two-dimensional field. FJ wants to enclose all of the cows with a rectangular fence whose sides is parallel to the X and Y axes, and he wants th It contains Everycow (cows on the boundary is allowed), fence to being as small as possible so. FJ is unfortunately in 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 does this is by building enclosures instead of one. Please help him compute how much less area he needs to enclose, in total, by using the enclosures instead of one. Like the original enclosure, the enclosures must collectively contain all the cows (with cows on boundaries allow ED), and they must has sides parallel to the X and Y axes. The enclosures is notallowed to overlap – not even on their boundaries. Note that enclosures of zero area isLegal, for example if an enclosure have zero width and/or zero height.
Input
The first line of input contains N. The NEXTNN lines each contain and integers specifying the location of a cow. Cow locations is positive integers in the range 1 ... 1,000,000,000
Outputwrite a single integer specifying amount of total area FJ can save by using the one, enclosures instead of one.
Sample Input6
4 2
8 10
1 1
9 12
14 7
2 3Sample Output107HINT Source
Test instructions: Give a few points to ask if covering all points with two rectangles will save much area than covering all points with a rectangle
The puzzle: ... Good pit ... The rectangle seems to intersect on the boundary but the topic says No. Then the area should be rectangular (x2-x1) * (y2-y1). There is no need to add one,.
First, sort all the points: Then we can consider enumerating the split lines of two rectangles: And because the dividing line can be horizontal or vertical ... So to enumerate two times
Specifically, you can see the code
#include <algorithm>#include<cstdio>#include<iostream>#include<cstring>#defineN 50015#defineDown (i,r,l) for (int i=r;i>=l;i--)#defineRep (i,l,r) for (int i=l;i<=r;i++)using namespaceStd;typedefLong Longll;intl[n],r[n],l1[n],r1[n],n;ll Zs,ans;structnode{intx, y;} S[n];BOOLCMP (node X,node y) {returnx.x==y.x?x.y<y.y:x.x<y.x;}voidGetans () {ll now; Sort (S+1, s+1+n,cmp); memset (L, -, (n+Ten) <<2); memset (L1, -, (n+Ten) <<2); Memset (R,0, (n+Ten) <<2); memset (R1,0, (n+Ten) <<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); }}intMain () {intMiny,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);}View Code
bzoj4578: [Usaco2016 open]splitting the Field