HDU1392 Surround the Trees
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
题目链接:HDU1392
正解:凸包
解题报告:
凸包模板题。
//It is made by ljh2000 //有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。 #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <complex> #include <vector> #include <cstdio> #include <string> #include <bitset> #include <queue> #include <cmath> #include <ctime> #include <map> #include <set> #define lc root<<1 #define rc root<<1|1 #define pr pair<int,int> #define MP make_pair #define fr first #define sc second #define rep(i,j,k) for(int i=j;i<=k;++i) #define per(i,j,k) for(int i=j;i>=k;--i) #define reg(i,x) for(int i=first[x];i;i=next[i]) using namespace std; typedef long long LL; typedef long double LB; typedef complex<double> C; const double pi = acos(-1); const double eps = 1e-9; const int MAXN = 1011; int n,top,stack[MAXN]; struct node{ double x,y; }a[MAXN]; inline node operator - (const node &q,const node &qq) { return (node){q.x-qq.x,q.y-qq.y}; } inline double cross(node q,node qq){ return q.x*qq.y-q.y*qq.x; } inline bool cmp(node q,node qq){ if(q.x==qq.x) return q.y<qq.y; return q.x<qq.x; } inline double sqr(LL x){ return x*x; } inline double dis(node q,node qq){ return sqrt(sqr(q.x-qq.x)+sqr(q.y-qq.y)); } inline int getint(){ int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar(); if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w; } inline void work(){ while(1) { n=getint(); if(n==0) break; for(int i=1;i<=n;i++) a[i].x=getint(),a[i].y=getint(); if(n==1) printf("%.2lf\n",0.00); else if(n==2) printf("%.2lf\n",dis(a[1],a[2])); else { sort(a+1,a+n+1,cmp); stack[top=1]=1; for(int i=2;i<=n;i++) { while(top>1 && cross( a[stack[top]]-a[stack[top-1]] , a[i]-a[stack[top-1]]) <= 0 ) top--; stack[++top]=i; } int k=top; for(int i=n;i>=1;i--) { while(top>k && cross( a[stack[top]]-a[stack[top-1]] , a[i]-a[stack[top-1]]) <= 0 ) top--; stack[++top]=i; } if(n!=1) top--; double ans=0; for(int i=1;i<=top/*!!!*/;i++) ans+=dis(a[stack[i]],a[stack[i+1]]); printf("%.2lf\n",ans); } } } int main() { #ifndef ONLINE_JUDGE freopen("1392.in","r",stdin); freopen("1392.out","w",stdout); #endif work(); return 0; } //有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!