题目链接:LightOJ 1203

Problem Description

Once there was a lazy monkey in a forest. But he loved banana too much. One day there was a storm in the jungle and all the bananas fell from the trees. The monkey didn't want to lose any of the bananas. So, he wanted to find a banana such that he can eat that and he can also look after the other bananas. As he was lazy, he didn't want to move his eyes too wide. So, you have to help him finding the banana from where he can look after all the bananas but the degree of rotating his eyes is as small as possible. You can assume that the position of the bananas can be modeled as 2D points.

Here a banana is shown, from where the monkey can look after all the bananas with minimum eye rotation.

Input

Input starts with an integer \(T (\le 13)\), denoting the number of test cases.

Each case starts with a line containing an integer \(n (1 \le n \le 105)\) denoting the number of bananas. Each of the next \(n\) lines contains two integers \(x y (-10^9 \le x, y \le 10^9)\) denoting the co-ordinate of a banana. There can me more than one bananas in the same co-ordinate.

Output

For each case, print the case number and the minimum angle in degrees. Errors less than \(10^-6\) will be ignored.

Sample Input

2
1
4 4
4
0 0
10 0
10 10
2 1

Sample Output

Case 1: 0
Case 2: 45.0000000

Note

Dataset is huge. Use faster I/O methods.

Solution

题意:

给定若干个点的坐标,求凸包最小顶角。

思路

凸包

先求凸包,然后枚举所有顶角求最小值。

顶角求法:用两个向量的夹角求

\(\angle BAC\) 为向量 \(\overrightarrow {AB}\) 与 \(\overrightarrow {AC}\) 的夹角:

\[cos<\overrightarrow {AB}, \overrightarrow {AC}> = \frac{\overrightarrow {AB} ⋅ \overrightarrow {AC}}{|\overrightarrow {AB}| |\overrightarrow {AC}|}
\]

Code

#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int maxn = 1e5 + 10; int n;
struct Point {
double x, y;
Point() {}
Point(double a, double b) : x(a), y(b) {}
bool operator<(const Point &b) const {
if (x < b.x) return 1;
if (x > b.x) return 0;
return y < b.y;
}
Point operator-(const Point &b) {
return Point(x - b.x, y - b.y);
}
} p[maxn], stk[maxn];
typedef Point Vec; int sgn(double x) {
if (fabs(x) <= eps)
return 0;
return x > 0 ? 1 : -1;
} double dist(Point a, Point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
} double cross(Vec a, Vec b) {
return a.x * b.y - a.y * b.x;
} int Andrew() {
sort(p + 1, p + 1 + n);
int len = 0;
for (int i = 1; i <= n; ++i) {
while (len > 1 && sgn(cross(stk[len] - stk[len - 1], p[i] - stk[len - 1])) == -1) {
len--;
}
stk[++len] = p[i];
}
int k = len;
for (int i = n - 1; i >= 1; --i) {
while (len > k && sgn(cross(stk[len] - stk[len - 1], p[i] - stk[len - 1])) == -1) {
len--;
}
stk[++len] = p[i];
}
return len;
} double angle(Point p, Point q, Point s) {
double x1 = q.x - p.x, y1 = q.y - p.y;
double x2 = s.x - p.x, y2 = s.y - p.y;
double ans = (x1 * x2 + y1 * y2) / (sqrt(x1 * x1 + y1 * y1) * sqrt(x2 * x2 + y2 * y2));
return acos(ans);
}
int main() {
int T;
scanf("%d", &T);
int kase = 0;
while(T--) {
map<pair<double, double>, int> mp;
n = 0;
int cnt;
scanf("%d", &cnt);
for (int i = 1; i <= cnt; ++i) {
double x, y;
scanf("%lf%lf", &x, &y);
if(mp[make_pair(x, y)] == 0) {
mp[make_pair(x, y)] = 1;
p[++n].x = x;
p[n].y = y;
}
} if(n < 3) {
printf("Case %d: 0\n", ++kase);
continue;
}
int t = Andrew();
double min_angle = angle(stk[1], stk[t - 1], stk[2]);
for (int i = 2; i < t; i++) {
min_angle = min(min_angle, angle(stk[i], stk[i - 1], stk[i + 1]));
}
printf("Case %d: %.6lf\n", ++kase, min_angle * 180.0 / pi);
}
return 0;
}

LightOJ 1203 Guarding Bananas (凸包最小顶角)的更多相关文章

  1. Guarding Bananas

    Guarding Bananas Once there was a lazy monkey in a forest. But he loved banana too much. One day the ...

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

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1203 题意:给你一个点集,求凸包中最小的角:模板题,但是刚开始的时候模板带错了,错的我 ...

  3. LightOJ 1239 - Convex Fence 凸包周长

    LINK 题意:类似POJ的宫殿围墙那道,只不过这道题数据稍微强了一点,有共线的情况 思路:求凸包周长加一个圆周长 /** @Date : 2017-07-20 15:46:44 * @FileNam ...

  4. LightOJ 1203--Guarding Bananas(二维凸包+内角计算)

    1203 - Guarding Bananas    PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 M ...

  5. kuangbin 带你飞 数学基础

    模版整理: 晒素数 void init() { cas = ; ; i < MAXD ; i++) is_prime[i] = true; is_prime[] = is_prime[] = f ...

  6. (hdu step 7.1.5)Maple trees(凸包的最小半径寻找掩护轮)

    称号: Maple trees Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  7. 【旋转卡壳+凸包】BZOJ1185:[HNOI2007]最小矩形覆盖

    1185: [HNOI2007]最小矩形覆盖 Time Limit: 10 Sec  Memory Limit: 162 MBSec  Special JudgeSubmit: 1945  Solve ...

  8. 【BZOJ1185】[HNOI2007]最小矩形覆盖(凸包,旋转卡壳)

    [BZOJ1185][HNOI2007]最小矩形覆盖(凸包,旋转卡壳) 题面 BZOJ 洛谷 题解 最小的矩形一定存在一条边在凸包上,那么枚举这条边,我们还差三个点,即距离当前边的最远点,以及做这条边 ...

  9. [BZOJ1185][HNOI2007]最小矩形覆盖-[凸包+旋转卡壳]

    Description 传送门 Solution 感性理解一下,最小矩形一定是由一条边和凸包上的边重合的. 然后它就是模板题了..然而真的好难调,小于大于动不动就打错. Code #include&l ...

随机推荐

  1. 通过HttpServletResponseWrapper修改response输出流

    在项目中遇到一个问题,需要对接口返回的数据进行加密给前端.项目中的controller一般都是返回一个实体form,重写的一个视图解析器继承ModelAndViewResolver,对返回的form转 ...

  2. HTML5: HTML5 Web SQL 数据库

    ylbtech-HTML5: HTML5 Web SQL 数据库 1.返回顶部 1. HTML5 Web SQL 数据库 Web SQL 数据库 API 并不是 HTML5 规范的一部分,但是它是一个 ...

  3. Perceptron Algorithm 感知器算法及其实现

    Rosenblatt于1958年发布的感知器算法,算是机器学习鼻祖级别的算法.其算法着眼于最简单的情况,即使用单个神经元.单层网络进行监督学习(目标结果已知),并且输入数据线性可分.我们可以用该算法来 ...

  4. python *arg和**kwargs的区别

    转载自:https://www.cnblogs.com/yunguoxiaoqiao/p/7626992.html 一.*args的使用方法 *args 用来将参数打包成tuple给函数体调用 举个栗 ...

  5. docker使用entrypoint执行时报permission denied错误

    在Dockerfile中使用指令ENTRYPOINT来执行项目下entrypoint.shshell文件,如下: ENTRYPOINT ["./entrypoint.sh"] 时报 ...

  6. Java转型大数据开发全套教程,都在这儿!

    众所周知,很多语言技术已经在长久的历史发展中掩埋,这期间不同的程序员也走出的自己的发展道路. 有的去了解新的发展趋势的语言,了解新的技术,利用自己原先的思维顺利改变自己的title. 比如我自己,也都 ...

  7. 25-python基础-python3-集合(set)常用操作

    sets 支持 x in set, len(set),和 for x in set.作为一个无序的集合,sets不记录元素位置或者插入点.因此,sets不支持 indexing, slicing, 或 ...

  8. python之序列去重以及生成器、生成器函数、生成器表达式与迭代器浅谈

    首先要明确序列值类型是否可哈希,因为可哈希的值很简单就可以用 in /not in 写个生成器去判断,如果是不可哈希的就要去转换为可哈希的再用 in/not in 去判断 原地不可变类型(可哈希): ...

  9. 8、服务发现&服务消费者Feign

    spring cloud的Netflix中提供了两个组件实现软负载均衡调用,分别是Ribbon和Feign.上一篇和大家一起学习了Ribbon. Ribbon :Spring Cloud Ribbon ...

  10. php 时间转化为刚刚、几秒前、几分前、几天前等等,友好时间提示

    / * 友好时间显示 */ function date_friend_tips($time){ if (!$time) return false; if(!is_numeric($time)){ $t ...