Share the Ruins Preservation(Graham算法利用叉积动态维护上下凸壳)
Posted xiaolaji
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Share the Ruins Preservation(Graham算法利用叉积动态维护上下凸壳)相关的知识,希望对你有一定的参考价值。
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.
输入
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.
输出
样例输入
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;
以上是关于Share the Ruins Preservation(Graham算法利用叉积动态维护上下凸壳)的主要内容,如果未能解决你的问题,请参考以下文章
MDT部署系统报错 connection to the deployment share (Mdtdeployent Shares)could not be made.The following ne
Note that ‘/home/w/.local/share‘ is not in the search pathset by the XDG_DATA_HOME and XDG_DATA_DIRS
U面经Prepare: Print Binary Tree With No Two Nodes Share The Same Column
Flutter笔记-There are multiple heroes that share the same tag within a subtree
Flutter踩坑记: There are multiple heroes that share the same tag within a subtree.
error: Two output files share the same path but have different contents: node_modules.vite..xxx.js