POJ 1113 凸包模板题

上模板。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <deque>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define INF 0x3f3f3f3f
#define MAXN 1005

using namespace std;

const double eps = 1e-8;
const double PI = acos(-1.0);
int sgn(double x)
{
    if(fabs(x) < eps) return 0;
    if(x < 0) return -1;
    return 1;
}
struct Point
{
    double x,y;
    Point(){}
    Point(double _x, double _y): x(_x),y(_y) {}
    Point operator -(const Point &B) const
    {
        return Point(x-B.x, y-B.y);
    }
    Point operator +(const Point &B) const //向量相加
    {
        return Point(x+B.x, y+B.y);
    }
    double operator ^(const Point &B) const //叉积
    {
        return x*B.y - y*B.x;
    }
    double operator *(const Point &B) const //点积
    {
        return x*B.x + y*B.y;
    }
    bool operator ==(const Point &B) const
    {
        return fabs(B.x-x)<eps && fabs(B.y-y)<eps;
    }
    bool operator !=(const Point &B) const
    {
        return !((*this) == B);
    }
    void transXY(double B) //绕原点逆时针旋转B弧度
    {
        double tx = x, ty = y;
        x = tx*cos(B) - ty*sin(B);
        y = tx*sin(B) + ty*cos(B);
    }
    void input() //读入只能用double读入
    {
        scanf("%lf%lf",&x,&y);
    }
};

double dist(Point a, Point b)
{
    return sqrt((a-b)*(a-b));
}

//求凸包,Graham算法
//点的编号0~n-1
//返回凸包结果Stack[0~top-1]为凸包的编号
//一个点或两个点 则凸包为一或二个点
int Stack[MAXN],top;
Point vertex[MAXN];
bool Graham_cmp(Point A, Point B)
{
    double tmp=(A-vertex[0])^(B-vertex[0]);
    if(sgn(tmp) > 0) return 1;
    if(sgn(tmp) == 0 && sgn(dist(A,vertex[0])-dist(B,vertex[0])) <= 0) return 1;
    return 0;
}
void Graham(int n)
{
    int k=0;
    for(int i=1; i<n; i++)
        if((vertex[k].y>vertex[i].y) || (vertex[k].y==vertex[i].y && vertex[k].x>vertex[i].x))
            k=i;
    swap(vertex[0], vertex[k]);
    sort(vertex+1, vertex+n, Graham_cmp);
    if(n == 1)
    {
        top=1;
        Stack[0]=0;
        return;
    }
    if(n == 2)
    {
        top=2;
        Stack[0]=0;
        Stack[1]=1;
        return;
    }
    Stack[0]=0;
    Stack[1]=1;
    top=2;
    for(int i=2; i<n; i++)
    {
        while(top > 1 && sgn((vertex[Stack[top-1]]-vertex[Stack[top-2]])^(vertex[i]-vertex[Stack[top-2]])) <= 0)
            top--;
        Stack[top++]=i;
    }
}

int main()
{
    int n,l;
    while(scanf("%d%d",&n,&l)!=EOF)
    {
        for(int i=0; i<n; i++)
            vertex[i].input();
        Graham(n);
        double ans=0.0;
        for(int i=0; i<top; i++)
            ans+=dist(vertex[Stack[i]],vertex[Stack[(i+1)%top]]);
        ans+=2*PI*l;
        printf("%.f\n",ans);
    }
    return 0;
}

  

POJ 1113 凸包模板题

时间: 2024-09-30 19:25:47

POJ 1113 凸包模板题的相关文章

POJ 3528 hdu 3662 三维凸包模板题

POJ 3528题:http://poj.org/problem?id=3528 HDU 3662:http://acm.hdu.edu.cn/showproblem.php?pid=3662 一个是求三维凸包面数,一个是求三维凸包表面积,都是很裸的. 贴代码: #include<stdio.h> #include<algorithm> #include<string.h> #include<math.h> #include<stdlib.h>

poj 1113 凸包周长

Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33888   Accepted: 11544 Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he w

poj 1113 Wall (凸包模板题)

Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32808   Accepted: 11137 Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he w

POJ - 1113 Wall (凸包模板题)

原题链接 模板题,直接说思路. 思路: 要求一距离凸包为 L 的图形的周长,即为 凸包周长+L为半径的圆周长 ,直接用 Graham 求一次凸包即可. 1 /* 2 * @Author: windystreet 3 * @Date: 2018-08-02 20:41:25 4 * @Last Modified by: windystreet 5 * @Last Modified time: 2018-08-02 22:30:59 6 */ 7 #include <stdio.h> 8 #inc

POJ Oulipo KMP 模板题

http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41051   Accepted: 16547 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a mem

hdu 1348 Wall(凸包模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 Wall Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3386    Accepted Submission(s): 968 Problem Description Once upon a time there was a gre

POJ Oulipo(KMP模板题)

题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<queue> #include<stack&

hdu4266(三维凸包模板题)

/*给出三维空间中的n个顶点,求解由这n个顶点构成的凸包表面的多边形个数. 增量法求解:首先任选4个点形成的一个四面体,然后每次新加一个点,分两种情况: 1> 在凸包内,则可以跳过 2> 在凸包外,找到从这个点可以"看见"的面,删除这些面, 然后对于一边没有面的线段,和新加的这个点新建一个面,至于这个点可以看见的面, 就是求出这个面的方程(可以直接求法向量). */ #include<iostream> #include<cmath> #includ

bzoj1670 Usaco2006 Building the Moat护城河的挖掘 [凸包模板题]

Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场周围挖一条护城河.农场里一共有N(8<=N<=5,000)股泉水,并且,护城河总是笔直地连接在河道上的相邻的两 股泉水.护城河必须能保护所有的泉水,也就是说,能包围所有的泉水.泉水一定在护城河的内部,或者恰好在河道上.当然,护城河构成一个封闭的环. 挖护城河是一项昂贵的工程,于是,节约的FJ希望护城河的总长度尽量小.请你写个程序计算一下,在满足需求的条件下,护城河的总长最小是多少. 所有泉水的坐标都在