Scan法求凸包

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348

给一个半径和n个点

求圆的周长 + n个点的凸包的周长

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const double pi = acos(-1.0);
struct node {
    double x,y;
}p[maxn],P[maxn]; 
int n,tot;
double l,ans;

//向量AB 和 AC求x积 如果X(A,B,C)>0 则AC在AB的左边
double X(node A,node B,node C) {
    return (B.x-A.x)*(C.y-A.y)-(B.y-A.y)*(C.x-A.x);
}
double len(node A,node B) { return sqrt((B.x-A.x)*(B.x-A.x)+(B.y-A.y)*(B.y-A.y)); }
bool cmp(node A,node B) { double pp = X(p[0],A,B); if(pp>0) return true; if(pp<0) return false; return len(p[0],A) < len(p[0],B); } void solve() { for(int i=0;i<n;i++) { if(p[i].y < p[0].y) swap(p[0],p[i]); else if(p[i].y==p[0].y && p[i].x < p[0].x) swap(p[0],p[1]); } sort(p+1,p+n,cmp); P[0]=p[0]; P[1]=p[1]; tot=1; for(int i=2;i<n;i++) { while (tot>0 && X(P[tot-1],P[tot],p[i])<=0) tot--; tot++; P[tot]=p[i]; } } int main () { int T; cin >> T; for(int cas=1;cas<=T;cas++) { if(cas!=1) puts(""); scanf("%d%lf",&n,&l); ans = 2*pi*l; for(int i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y); if(n==1) printf("%.0f ",ans); if(n==2) printf("%.0f ",ans+len(p[0],p[1])); else { solve(); for(int i=0;i<tot;i++) { ans += len(P[i],P[i+1]); } ans += len(P[0],P[tot]); printf("%.0f ",ans); } } return 0; }

  

原文地址:https://www.cnblogs.com/Draymonder/p/9478368.html