题意:
求把城堡围起来需要的最小墙壁周长。
思路:
围墙周长为=n条平行于凸包的线段+n条圆弧的长度=凸包周长+围墙离城堡距离L为半径的圆周长。
代码:
。。。还是看大佬写的,自己做个记录方便日后复习。。
#include#include #include #include #include using namespace std;const int N=2e5+20;const double PI=acos(-1.0);struct point{ int x,y;}p[N];int n;int stack[N],top;int cross(point p0,point p1,point p2)//p0p1 * p0p2叉积 判断顺/逆时针{ return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);}double dis(point p1,point p2){ return sqrt((double)(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));}bool cmp(point p1,point p2)//极角排序 p[0]为最下方&&最左边的点{ int tmp=cross(p[0],p1,p2); if(tmp>0) return true; else if(tmp==0&&dis(p[0],p1) 2) { for(int i=0;i<=1;i++) stack[i]=i; top=1; for(int i=2;i 0&&cross(p[stack[top-1]],p[stack[top]],p[i])<=0) top--; top++; stack[top]=i; } }}int main(){ double L; while(cin>>n>>L) { int low=0; for(int i=0;i >p[i].x>>p[i].y; if((p[low].y==p[i].y&&p[low].x>p[i].x)||p[low].y>p[i].y) low=i; } swap(p[0],p[low]);//p[0]为最下方&&最左边的点 sort(p+1,p+n,cmp); Graham(n); double res=0; for(int i=0;i