Language: Default
Wall
Description Input
第一行为 N
(3 <= N <= 1000) 和 L(1 <= L <= 1000).
接下来 N 行,每行为城堡坐标Xi,Yi (-10000 <= Xi, Yi <= 10000).
Output
输出一行Wall最短长度.
Sample Input 9 100 200 400 300 400 300 300 400 300 400 400 500 400 500 200 350 200 200 200 Sample Output 1628 Hint
结果四舍五入就可以了
Source
Northeastern Europe 2001
|
易证Wall最短长度=对城堡求凸包的周长+2πL
这里介绍一个凸包求法QuickHull
备注Quickhull-
s表示向量CA与CB的叉积(在上一求得,用于求距离),初始化为0
用[l,r]表示点集区间,
有2个要点
1.A,B , C的关系
(1)
(2)
2.i,j的调换
可从下图看出
#include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<cmath> #include<functional> #include<algorithm> #include<cctype> using namespace std; #define MAXN (1000+10) #define MAXXi (10000+10) #define eps (1e-10) const double pi=3.1415926; int sqr(int x) {return x*x;} struct P { int x,y; P(){} P(int _x,int _y):x(_x),y(_y){} friend istream& operator>>(istream &cin,P &a) { cin>>a.x>>a.y; return cin; } friend bool operator<(const P a,const P b){return (a.x==b.x)?a.y<b.y:a.x<b.x;} friend bool operator>(const P a,const P b){return (a.x==b.x)?a.y>b.y:a.x>b.x;} }a[MAXN],st[MAXN]; double dis(P A,P B) { return sqrt((double)sqr(A.x-B.x)+sqr(A.y-B.y)); } struct V { int x,y; V(){} V(int _x,int _y):x(_x),y(_y){} V(P a,P b):x(b.x-a.x),y(b.y-a.y){} friend int operator*(V a,V b) { return a.x*b.y-a.y*b.x; } }; int n,l,size=0; double s[MAXN]={0.0}; void hull(int l,int r,P A,P B) { int p=l; for (int k=l;k<=r;k++) if (s[p]<s[k]||s[p]==s[k]&&a[p]<a[k]) p=k; P C=a[p]; int i=l-1,j=r+1; for (int k=l;k<=r;k++) { s[++i]=V(a[k],A)*V(a[k],C); if (s[i]>0) /*Right*/ swap(a[i],a[k]); else i--; } for (int k=r;k>=l;k--) { s[--j]=V(a[k],C)*V(a[k],B); if (s[j]>0) /*Right*/ swap(a[j],a[k]); else j++; } if (l<=i) hull(l,i,A,C); st[++size]=C; if (j<=r) hull(j,r,C,B); } int main() { // freopen("poj1113.in","r",stdin); cin>>n>>l; for (int i=1;i<=n;i++) cin>>a[i]; int pmin=1,pmax=1; for (int i=2;i<=n;i++) { if (a[i]<a[pmin]) pmin=i; if (a[i]>a[pmax]) pmax=i; } swap(a[1],a[pmin]);swap(a[n],a[pmax]); st[++size]=a[1]; hull(2,n,a[1],a[1]); // for (int i=1;i<=size;i++) cout<<st[i].x<<' '<<st[i].y<<' '<<endl; double ans=0; for (int i=1;i<=size;i++) ans+=dis(st[i],st[i+1<=size?i+1:1]); cout.setf(ios::fixed); cout.precision(0); cout<<ans+2*pi*l<<endl; return 0; }