LightOj1203 - Guarding Bananas(凸包求多边形中的最小角)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1203

题意:给你一个点集,求凸包中最小的角;模板题,但是刚开始的时候模板带错了,错的我都想吐了;

#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define met(a, b) memset(a, b, sizeof(a))
const double eps = 1e-10;
const double PI = acos(-1);
const int N = 150010;

struct point
{
    double x, y;
    point(double x=0, double y=0) : x(x), y(y){}
    friend point operator - (const point& p1, const point& p2)
    {
        return point(p1.x-p2.x, p1.y-p2.y);
    }
    friend double operator ^ (const point& p1, const point& p2)
    {
        return p1.x*p2.y - p1.y*p2.x;
    }
}p[N], res[N];
double Dist(point p1, point p2)
{
    double dx = p1.x - p2.x, dy = p1.y - p2.y;
    return sqrt(dx*dx + dy*dy);
}
bool cmp1(point p1, point p2)
{
    if(p1.y == p2.y)
        return p1.x < p2.x;
    return p1.y < p2.y;
}
bool 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)
{
    res[0] = p[0];if(n == 1) return 1;
    res[1] = p[1];if(n == 2) return 2;
    int top = 1;
    for(int i=2; i<n; i++)
    {
        while(top && ((res[top]-res[top-1])^(p[i]-res[top-1])) <= 0) top--;
        res[++top] = p[i];
    }
    return top+1;
}
int main()
{
    int n, T, tCase = 1;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
        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,保存在res中,下标从0开始;
        if(cnt < 3)
        {
            printf("Case %d: 0\n", tCase++);
            continue;
        }
        double ans = 1000;
        res[cnt] = res[0], res[cnt+1] = res[1];
        for(int i=1; i<=cnt; i++)
        {
            double a = Dist(res[i-1], res[i+1]);
            double b = Dist(res[i-1], res[i]);
            double c = Dist(res[i], res[i+1]);
            ans = min(ans, acos((b*b+c*c-a*a)/(2*b*c)));
        }
        printf("Case %d: %.6f\n", tCase++, ans*180/PI);
    }
    return 0;
}

时间: 2024-08-02 03:06:12

LightOj1203 - Guarding Bananas(凸包求多边形中的最小角)的相关文章

poj 3348 Cows 凸包 求多边形面积 计算几何 难度:0 Source:CCC207

Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7038   Accepted: 3242 Description Your friend to the south is interested in building fences and turning plowshares into swords. In order to help with his overseas adventure, they are f

Cows 计算几何 求凸包 求多边形面积

题目链接:https://cn.vjudge.net/problem/POJ-3348 题意 啊模版题啊 求凸包的面积,除50即可 思路 求凸包的面积,除50即可 提交过程 AC 代码 #include <cmath> #include <cstdio> #include <vector> #include <algorithm> using namespace std; const double eps=1e-10; struct Point{ doubl

求数组中绝对值最小的元素

给定一个有序数组a(从小到大排列),数组中的数据有正有负,找出这个数组中的绝对值最小的元素.最先到的自然是从头到尾依次遍历数组中的每个元素,找出绝对值最小的元素.这是最简单的方法,不过它并没有用到数组有序这个特性,现在我们来看看有没有更好的方法.题目要求在数组中查找元素,并且此数组有序,那么可以想到用二分法来处理. 首先我们先看一下如果数组中元素全部为正数或者全部为负数的情况: 如果a[0]>0,那么数组中所有元素均为正数,则a[0]为绝对值最小的元素 如果a[len-1]<0,那么数组中所有

c++求数组中的最小(大)的n位数

#include <iostream> using namespace std; class MaxHash { public: MaxHash(int n) { data = new int[n]; size = n; } void Insert(int a[], int n) { int i = 0; for (; i < size; i++)//初始化数组 { data[i] = a[i]; } int j = size / 2; while (j >= 0)//构造堆 {

[经典面试题]排序数组中绝对值最小元素

[题目] 题目为: 有一个已经排序的数组(升序),数组中可能有正数.负数或0,求数组中元素的绝对值最小的数,要求,不能用顺序比较的方法(复杂度需要小于O(n)),可以使用任何语言实现 例如,数组{-20,-13,-4, 6, 77,200} ,绝对值最小的是-4. [分析] 给定数组是已经排好序的,且是升序,没有重复元素. 一个简单的思路,就是一次性遍历数组,求出数组的元素的绝对值的最小值,这样的时间复杂度为O(n). 但是,这样就浪费了题目的一个条件:数组是已经排好序的.所以,需要对原来的题目

【简单凸包】LightOJ 1203 Guarding Bananas

[简单凸包]LightOJ 1203 Guarding Bananas 题目链接:LightOJ 1203 Guarding Bananas 题目大意 构造凸包,求凸包夹角的最小值 笔者的第一道凸包题目,发现Kuangbin的计算几何模板的一个最大缺陷:结构体太长,大空间开不下QAQ 凸包,我的理解是包含已知点集的最小凸集,二维凸包自然可以理解为包含所有点的最小凸多边形. 现在代码的逼格越来越高了~(≧▽≦)/~啦啦啦! 说一下思路 ①Graham's Scan法构建凸包,时间复杂度O(nlog

hdu3685 Rotational Painting 求多边形重心和凸包

http://acm.hdu.edu.cn/showproblem.php?pid=3685 Rotational Painting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2614    Accepted Submission(s): 737 Problem Description Josh Lyman is a gifted

opencv---(腐蚀、膨胀、边缘检测、轮廓检索、凸包、多边形拟合)

一.腐蚀(Erode) 取符合模板的点, 用区域最小值代替中心位置值(锚点) 作用: 平滑对象边缘.弱化对象之间的连接. opencv 中相关函数:(erode) 1 // C++ 2 /** 3 shape: 形状 4 MORPH_RECT 矩形 5 MORPH_CROSS 交叉形 十字型 6 MORPH_ELLIPSE 椭圆形 7 esize : 大小 8 anchor: 锚点,默认为中心 9 **/ 10 Mat getStructuringElement(int shape, Size

poj1408——叉积求多边形面积

poj1408——叉积求多边形面积 Fishnet Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1853   Accepted: 1185 Description A fisherman named Etadokah awoke in a very small island. He could see calm, beautiful and blue sea around the island. The previou