传送门
题意:几乎是模板题,求凸包的周长
用Graham扫描法解决(还有一个叫jarvis步行法,目测也差不多)。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=102;
struct NODE {
int x,y;
}a[maxn],s[maxn];
int top,n;
inline int read() {
int x=0;char c=getchar();
while (c<'0'||c>'9') c=getchar();
while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
return x;
}
inline double dis(NODE a,NODE b) {
return sqrt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
inline int cross(NODE a,NODE b,NODE c) {
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
inline int cmp1(NODE a,NODE b) {
return a.y<b.y||(a.y==b.y&&a.x<b.x);
}
inline int cmp2(NODE c,NODE d) {
int temp=cross(a[0],c,d);
if (temp==0) return dis(a[0],c)<dis(a[0],d);//three points collinear
return temp>0;
}
int main() {
// freopen("hdu 1392.in","r",stdin);
while (n=read()) {
for (int i=0;i<n;++i) a[i].x=read(),a[i].y=read();
if (n==1||n==2) {printf("%.2lf\n",n^1?dis(a[0],a[1]):0.0);continue;}//special judge
sort(a,a+n,cmp1),sort(a+1,a+n,cmp2),
top=-1,
s[++top]=a[0],s[++top]=a[1],s[++top]=a[2];
for (int i=3;i<n;++i) {
while (top&&cross(s[top-1],s[top],a[i])<=0) --top;
//strictly '<=', if three points are collinear, choose the first ans the last
s[++top]=a[i];
}
double c=0.0;
for (int i=1;i<=top;++i) c+=dis(s[i-1],s[i]);
c+=dis(s[top],s[0]);
printf("%.2lf\n",c);
}
return 0;
}