1. 程式人生 > >洛谷P3145 [USACO16OPEN]分割田地Splitting the Field

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

fff cows pro and original see ota therefore nat

P3145 [USACO16OPEN]分割田地Splitting the Field

題目描述

Farmer John‘s NN cows (3 \leq N \leq 50,0003N50,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,00011,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,127,sizeof(l));
    memset(r,-127,sizeof(r));
    memset(l1,127,sizeof(l1));
    memset(r1,-127,sizeof(r1));
    sort(p+1,p+n+1);
    for(int i=1;i<=n;i++){
        l[i]=min(l[i-1],p[i].y);
        r[i]=max(r[i-1],p[i].y);
    }
    for(int i=n;i>=1;i--){
        l1[i]=min(l1[i+1],p[i].y);
        r1[i]=max(r1[i+1],p[i].y);
    }
    for(int i=2;i<=n;i++){
        long long tem=1LL*(r[i-1]-l[i-1])*(p[i-1].x-p[1].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=1;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=1;i<=n;i++)swap(p[i].x,p[i].y);
    work();
    cout<<ans1-ans;
    return 0;
}

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