POJ 1113 凸包模版题

31 篇文章 0 订阅

题目: 题目链接

题目的意思就是让你求出凸包,然后在一凸包向外延伸L米。问此时的环的长度是多少?

#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <map>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cmath>
#include <queue>
#include <iomanip>
using namespace std;


const int inf=10001;
const double pi=3.141592654;

typedef class
{
public:
    int x,y;
} point;

/*AB距离平方*/

int distsquare(point A,point B)
{
    return (B.x-A.x)*(B.x-A.x)+(B.y-A.y)*(B.y-A.y);
}

/*AB距离*/

double distant(point A,point B)
{
    return sqrt((double)((B.x-A.x)*(B.x-A.x)+(B.y-A.y)*(B.y-A.y)));
}

/*叉积计算*/

int det(int x1,int y1,int x2,int y2)
{
    return x1*y2-x2*y1;
}

int cross(point A,point B,point C,point D)
{
    return det(B.x-A.x,B.y-A.y,D.x-C.x,D.y-C.y);
}

/*快排判断规则*/

point* s;
int cmp(const void* pa,const void* pb)
{
    point* a=(point*)pa;
    point* b=(point*)pb;

    int temp=cross(*s,*a,*s,*b);//利用左转关系排序
    if(temp>0)
        return -1;
    else if(temp==0)//如果相同,按照距离来排序
        return distsquare(*s,*b)-distsquare(*s,*a);
    else
        return 1;
}

int main()
{
    int i, j;
    int N,L;
    while(cin>>N>>L)
    {
        point* node=new point[N+1];
        int min_x=inf;
        int fi;
        for(i=1; i<=N; i++)
        {
            cin>>node[i].x>>node[i].y;

            if(min_x > node[i].x)
            {
                min_x = node[i].x;
                fi=i;
            }
            else if(min_x == node[i].x)
                if(node[fi].y > node[i].y)
                    fi=i;
        }

        /*Quicksort the Vertex*/

        node[0]=node[N];
        node[N]=node[fi];
        node[fi]=node[0];

        s=&node[N];
        qsort(node+1,N,sizeof(point),cmp);

        /*Structure Con-bag*/

        int* bag=new int[N+2];
        bag[1]=N;
        bag[2]=1;
        int pb=2;
        for(i=2; i<=N;)//入队
            if(cross(node[ bag[pb-1] ],node[ bag[pb] ],node[ bag[pb] ],node[i]) >= 0)
                bag[++pb]=i++;//左转入队
            else
                pb--;//非左转就一直退栈

        /*Compute Min-length*/

        double minlen=0;
        for(i=1; i<pb; i++)
            minlen+=distant(node[ bag[i] ],node[ bag[i+1] ]);

        minlen+=2*pi*L;

        cout<<fixed<<setprecision(0)<<minlen<<endl;//C++的输出

        delete node;
        delete bag;
    }
    return 0;
}

努力努力.....

几何题目还是需要模版啊...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值