HDU 1392 Surround the Trees

来源:互联网 发布:卡佩拉毒蛇队数据 编辑:程序博客网 时间:2024/06/05 09:00

平面凸包

不过注意下两个点的时候 只输出距离就可以了 我也不知道为什么 没读懂题意 英文捉急!

#include <cstdio>#include <cmath>#include <iostream>#include <algorithm>using namespace std;struct H{    int x,y;}a[1010];int team1[1010],team2[1010],top1,top2;int CHA(H a,H b,H c){    int x1=b.x-a.x;    int y1=b.y-a.y;    int x2=c.x-b.x;    int y2=c.y-b.y;    return x1*y2-x2*y1;}double D(H a,H b){    double y=a.y-b.y;    double x=a.x-b.x;    return sqrt(y*y+x*x);}bool cmp(H a, H b){     if(a.x!=b.x) return a.x<b.x;     return a.y<b.y;}int main(){//    freopen("a.in","r",stdin);//    freopen("wa.out","w",stdout);    int n;    while(scanf("%d",&n)&&n!=0)    {        for(int i=1;i<=n;i++)        {            scanf("%d%d",&a[i].x,&a[i].y);        }        if(n==1)        {            printf("0.00\n");            continue;        }        if(n==2)        {            printf("%.2f\n",D(a[1],a[2]));            continue;        }    //    cout<<"faf";        sort(a+1,a+n+1,cmp);        top1=top2=0;        for(int i=1;i<=n;i++)        {            while(a[i].x==a[i+1].x&&i+1<=n)             {                i++;            }            while(top1!=1&&top1!=0&&CHA(a[team1[top1-1]],a[team1[top1]],a[i])>=0)                top1--;            team1[++top1]=i;        }        for(int i=1;i<=n;i++)        {            while(top2!=1&&top2!=0&&CHA(a[team2[top2-1]],a[team2[top2]],a[i])<=0)                top2--;            team2[++top2]=i;            while(a[i].x==a[i+1].x&&i+1<=n)             {                i++;                }        }            double ans=0;        for(int i=1;i<top1;i++)            ans+=D(a[team1[i]],a[team1[i+1]]);        for(int i=1;i<top2;i++)            ans+=D(a[team2[i]],a[team2[i+1]]);        ans+=D(a[team2[1]],a[team1[1]]);        ans+=D(a[team2[top2]],a[team1[top1]]);        printf("%.2f\n",ans);    }    return 0;} 
0 0
原创粉丝点击