码迷,mamicode.com
首页 > 编程语言 > 详细

Share the Ruins Preservation(Graham算法利用叉积动态维护上下凸壳)

时间:2019-09-14 00:45:45      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:clu   pos   splay   struct   nbsp   contain   hide   输入   arch   

Two organizations International Community for Preservation of Constructions (ICPC) and Japanese Archaeologist Group (JAG) engage in ruins preservation. Recently, many ruins were found in a certain zone. The two organizations decided to share the preservation of the ruins by assigning some of the ruins to ICPC and the other ruins to JAG.

Now, ICPC and JAG make a rule for assignment as follows:

1.Draw a vertical straight line from the north to the south, avoiding to intersect ruins.
2.Ruins located to the west of the line are preserved by ICPC. On the other hand, ruins located to the east of the line are preserved by JAG. (It is possible that no ruins are located to the east/west of the line; in this case, ICPC/JAG will preserve no ruins.)
A problem is where to draw a straight line. For each organization, the way to preserve its assigned ruins is to make exactly one fence such that all the assigned ruins are in the region surrounded by the fence. Furthermore, they should minimize the length of such a fence for their budget. If the surrounded areas are vast, expensive costs will be needed to maintain the inside of areas. Therefore, they want to minimize the total preservation cost, i.e. the sum of the areas surrounded by two fences. Your task is to write a program computing the minimum sum of the areas surrounded by two fences, yielded by drawing an appropriate straight line.

 

输入

The input consists of a single test case.

N
x1 y1
x2 y2
...
xN yN

The first line contains an integer N (1≤N≤100,000), which is the number of founded ruins. The following N lines represent the location of the ruins. The i-th line of them consists of two integers xi and yi, which indicate the location of the i-th ruin is xi east and yi north from a certain location in the zone. You can assume the following things for the ruins:

−109≤xi,yi≤109
You can ignore the sizes of ruins. That is, you can assume ruins are points.
No pair of ruins has the same location.

 

输出

Print the minimum total preservation cost yielded by drawing an appropriate straight line. You should round off the cost to the nearest integer.

 

样例输入

8
-10 0
-10 5
-5 5
-5 0
10 0
10 -5
5 -5
5 0

样例输出

50
技术图片
#include <bits/stdc++.h>
using namespace std;
const long double eps=1e-8;
const int maxn=1e5+10;
typedef __int128  ll;
struct point
{
    ll x,y;
} a[maxn];
bool cmp(point r,point t)
{
    if(r.x==t.x)
        return r.y<t.y;
    return r.x<t.x;
}
bool mult(point sp,point ep,point op)
{
    return (sp.x-op.x)*(ep.y-op.y)>=(ep.x-op.x)*(sp.y-op.y);;
}
ll area(point a,point b){
    return a.x*b.y-a.y*b.x;
}
vector<long double>posi;
vector<point>up,down;
__int128 le_ri[maxn<<1],ri_le[maxn<<1];
int main()
{
    int n,cnt;
    scanf("%d",&n);
    long long tx,ty;
    for(int i=1; i<=n; i++)
    {
        scanf("%lld%lld",&tx,&ty);
        a[i].x=tx;
        a[i].y=ty;
    }
    sort(a+1,a+1+n,cmp);
    for(int i=1; i<=n; i++)
    {
        if(i==1||a[i].x!=a[i-1].x)
        {
            posi.push_back(a[i].x-eps);
            posi.push_back(a[i].x+eps);
        }
    }
    int l=0;
    for(int i=0; i<posi.size(); i++)
    {
        __int128 changeup=0,changedown=0;
        for(int j=l+1; j<=n; j++)
        {
            if(a[j].x<posi[i])
            {
                if(up.size()==0)
                {
                    up.push_back(a[j]);
                }
                else if(up.size()==1)
                {
                    up.push_back(a[j]);
                    changeup+=area(up[1],up[0]);
                }
                else
                {
                    while(up.size()>=2&&!mult(a[j],up[up.size()-1],up[up.size()-2]))
                    {
                        changeup-=area(up[up.size()-1],up[up.size()-2]);
                        up.pop_back();
                    }
                    up.push_back(a[j]);
                    changeup+=area(up[up.size()-1],up[up.size()-2]);
                }
                if(down.size()==0)
                {
                    down.push_back(a[j]);
                }
                else if(down.size()==1)
                {
                    down.push_back(a[j]);
                    changedown+=area(down[0],down[1]);
                }
                else
                {
                    while(down.size()>=2&&mult(a[j],down[down.size()-1],down[down.size()-2]))
                    {
                        changedown-=area(down[down.size()-2],down[down.size()-1]);
                        down.pop_back();
                    }
                    down.push_back(a[j]);
                    changedown+=area(down[down.size()-2],down[down.size()-1]);
                }
                l=j;
            }
            else
            {
                break;
            }
        }
        if(i==0)
            le_ri[i]=changedown+changeup;
        else
            le_ri[i]=le_ri[i-1]+changedown+changeup;
    }
    down.clear();
    up.clear();
    int r=n+1;
    for(int i=posi.size()-1; i>=0; i--)
    {
        __int128 changeup=0,changedown=0;
        for(int j=r-1; j>=1; j--)
        {
            if(a[j].x>posi[i])
            {
                if(up.size()==0)
                {
                    up.push_back(a[j]);
                }
                else if(up.size()==1)
                {
                    up.push_back(a[j]);
                    changeup+=area(up[1],up[0]);
                }
                else
                {
                    while(up.size()>=2&&!mult(a[j],up[up.size()-1],up[up.size()-2]))
                    {
                        changeup-=area(up[up.size()-1],up[up.size()-2]);
                        up.pop_back();
                    }
                    up.push_back(a[j]);
                    changeup+=area(up[up.size()-1],up[up.size()-2]);
                }
                if(down.size()==0)
                {
                    down.push_back(a[j]);
                }
                else if(down.size()==1)
                {
                    down.push_back(a[j]);
                    changedown+=area(down[0],down[1]);
                }
                else
                {
                    while(down.size()>=2&&mult(a[j],down[down.size()-1],down[down.size()-2]))
                    {
                        changedown-=area(down[down.size()-2],down[down.size()-1]);
                        // cout<<down[down.size()-1].x<<" "<<down[down.size()-1].y<<" "<<down[down.size()-2].x<<" "<<down[down.size()-2].y<<endl;
                        down.pop_back();
                    }
                    down.push_back(a[j]);
                    changedown+=area(down[down.size()-2],down[down.size()-1]);
                }
                r=j;
            }
            else
            {
                break;
            }
        }
        if(i==posi.size()-1)
            ri_le[i]=changedown+changeup;
        else
            ri_le[i]=ri_le[i+1]+changedown+changeup;
    }
    __int128 ans=9000000000000000000;
    for(int i=0; i<posi.size(); i++)
    {
        ans=min(ans,le_ri[i]+ri_le[i]);
    }
    printf("%lld\n",(long long )((ans+1)/2));
    return 0;
}
View Code

 

 

Share the Ruins Preservation(Graham算法利用叉积动态维护上下凸壳)

标签:clu   pos   splay   struct   nbsp   contain   hide   输入   arch   

原文地址:https://www.cnblogs.com/xiaolaji/p/11517935.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!