Wall---hdu1348(求凸包周长 模板)

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

求凸包周长+2*PI*L

#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int N = 110;
const double eps = 1e-6;
const double PI = acos(-1);
struct point
{
    double x, y;
    point(){}
    point(double x, double y) : x(x), y(y) {}
    point friend operator - (const point &p1, const point &p2)///矢量p2p1;
    {
        return point(p1.x-p2.x, p1.y-p2.y);
    }
    double friend operator ^ (const point &p1, const point &p2)///p1×p2;
    {
        return p1.x*p2.y - p1.y*p2.x;
    }
};

point p[N], res[N];

double Dist(point p1, point p2)
{
    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}

int cmp1(point p1, point p2)///位置排序,找到最下方的;
{
    if(p1.y != p2.y)
        return p1.y < p2.y;
    return p1.x < p2.x;///若有多个下方的找左边的;
}
int cmp2(point p1, point p2)///极角排序;若极角相同,距离近的在前面;
{
    double k = (p1-p[0])^(p2-p[0]);
    if( k>eps || (fabs(k)<eps && Dist(p1, p[0]) < Dist(p2, p[0]) ))
        return 1;
    return 0;
}

int Graham(int n)///构造凸包,O(nlogn)
{
    res[0] = p[0];
    res[1] = p[1];
    res[2] = p[2];
    int top = 2;
    for(int i=3; i<n; i++)
    {
        while(((p[i]-res[top])^(res[top-1]-res[top])) <= 0)top--;
        ///当res[top-1]->res[top]->p[i],出现右拐或直行时没说明res[top]不是凸包上的点;
        res[++top] = p[i];
    }
    return top;
}

int main()
{
    int n, T;
    double L;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%lf", &n, &L);
        for(int i=0; i<n; i++)
            scanf("%lf %lf", &p[i].x, &p[i].y);

        sort(p, p+n, cmp1);///p[0]为最下方靠左的点;
        sort(p+1, p+n, cmp2);///以p[0]为基点,按叉积进行排序;

        int cnt = Graham(n);///求凸包的顶点个数cnt+1,保存在res中,下标从0开始;

        double ans = Dist(res[0], res[cnt]);
        for(int i=1; i<=cnt; i++)
            ans += Dist(res[i], res[i-1]);
        ans += PI*2*L;
        printf("%.0f\n", ans);
        if(T)puts("");
    }
    return 0;
}
时间: 2024-10-19 21:42:12

Wall---hdu1348(求凸包周长 模板)的相关文章

POJ 1113 Wall(Graham求凸包周长)

题目链接 题意 : 求凸包周长+一个完整的圆周长. 因为走一圈,经过拐点时,所形成的扇形的内角和是360度,故一个完整的圆. 思路 : 求出凸包来,然后加上圆的周长 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <cmath> 5 #include <algorithm> 6 7 const double PI = acos(-1.0) ;

poj1113Wall 求凸包周长 Graham扫描法

#include<iostream> #include<algorithm> #include<cmath> using namespace std; typedef pair<int ,int > ll; ll num,dot[1010]; int i; const double pi=3.1415926535898; ll operator -(ll a,ll b) { return make_pair(a.first-b.first,a.second-

HDU 1392 Surround the Trees (Graham求凸包周长)

题目链接 题意 : 让你找出最小的凸包周长 . 思路 : 用Graham求出凸包,然后对每条边求长即可. Graham详解 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <math.h> 5 #include <algorithm> 6 7 using namespace std ; 8 9 struct point 10 { 11 int

poj1873(二进制枚举+求凸包周长)

题目链接:https://vjudge.net/problem/POJ-1873 题意:n个点(2<=n<=15),给出n个点的坐标(x,y).价值v.做篱笆时的长度l,求选择哪些点来做篱笆围住另一些点,使得选出的这些点的价值和最小,如果价值和相等要求个数最小. 思路: 看来这是WF的签到题吧.数据很小,直接二进制枚举 (1<<n),然后对未选出的点求凸包的周长,仅当选出点的长度l的和>=凸包周长时才更新答案. AC code: #include<cstdio>

HDU 1392.Surround the Trees【凸包(求凸包周长)】【5月10】

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9790    Accepted Submission(s): 3763 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to

(模板)poj1113(graham扫描法求凸包)

题目链接:https://vjudge.net/problem/POJ-1113 题意:简化下题意即求凸包的周长+2×PI×r. 思路:用graham求凸包,模板是kuangbin的. AC code: #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int maxn=1005; const double PI=

Wall POJ - 1113 (凸包周长)

题目链接:https://vjudge.net/problem/POJ-1113 题目: 思路:就是求凸包周长加上一个圆周长 原文地址:https://www.cnblogs.com/Vampire6/p/12234411.html

[codevs 1298] 凸包周长 [codevs 3201] 奶牛代理商 XI

题解: 今天开始学习计算几何. 这是一道计算几何求凸包周长的模板题,采用Andrew算法. 第二道题改下输出即可. 最后凸包周长的求法注意第一个点和最后一个点是同一个. 代码 100ms 3MB #include<cstdio> #include<cmath> #include<vector> #include<algorithm> using namespace std; const int maxn = 100000 + 10; int n; struc

HDU 1392 Surround the Trees (凸包周长)

题目链接:HDU 1392 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you