P2742-二维凸包/圈奶牛Fencing the Cows【凸包】
生活随笔
收集整理的這篇文章主要介紹了
P2742-二维凸包/圈奶牛Fencing the Cows【凸包】
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
正題
題目鏈接:https://www.luogu.org/recordnew/lists?uid=SSL_WYC_zombieeeeee&pid=P2742&status=&sort=0
題目大意
求凸包總長度
解題思路
求凸包
codecodecode
#include<cstdio> #include<algorithm> #include<cmath> #define N 10010 using namespace std; struct point{double x,y; }a[N]; int n,s[N]; double ans; double m(point x,point y,point z) {return (x.x-z.x)*(y.y-z.y)-(y.x-z.x)*(x.y-z.y);} double dis(point x,point y) {return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));} bool cmp(point x,point y) {double t=m(x,y,a[1]);if(t>0||t==0&&dis(x,a[1])<dis(y,a[1]))return true;else return false; } void init() {scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%lf%lf",&a[i].x,&a[i].y);if(a[i].y<a[1].y||a[i].y==a[1].y&&a[i].x<a[1].x)swap(a[i],a[1]);}sort(a+2,a+1+n,cmp); } void praham() {s[1]=1;s[2]=2;s[3]=3;int top=3;for(int i=4;i<=n;i++){while(m(a[i],a[s[top]],a[s[top-1]])>=0)top--;s[++top]=i;}for(int i=1;i<top;i++)ans+=dis(a[s[i]],a[s[i+1]]);printf("%.2lf",ans+dis(a[s[1]],a[s[top]])); } int main() {init();praham(); }總結
以上是生活随笔為你收集整理的P2742-二维凸包/圈奶牛Fencing the Cows【凸包】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: xbox手柄如何连接电脑电脑上如何接手柄
- 下一篇: 如何删除Windows中的顽固文件如何删