题目链接: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

  1. 2
  2. 1
  3. 4 4
  4. 4
  5. 0 0
  6. 10 0
  7. 10 10
  8. 2 1

Sample Output

  1. Case 1: 0
  2. 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

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const double eps = 1e-8;
  4. const double pi = acos(-1.0);
  5. const int maxn = 1e5 + 10;
  6. int n;
  7. struct Point {
  8. double x, y;
  9. Point() {}
  10. Point(double a, double b) : x(a), y(b) {}
  11. bool operator<(const Point &b) const {
  12. if (x < b.x) return 1;
  13. if (x > b.x) return 0;
  14. return y < b.y;
  15. }
  16. Point operator-(const Point &b) {
  17. return Point(x - b.x, y - b.y);
  18. }
  19. } p[maxn], stk[maxn];
  20. typedef Point Vec;
  21. int sgn(double x) {
  22. if (fabs(x) <= eps)
  23. return 0;
  24. return x > 0 ? 1 : -1;
  25. }
  26. double dist(Point a, Point b) {
  27. return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
  28. }
  29. double cross(Vec a, Vec b) {
  30. return a.x * b.y - a.y * b.x;
  31. }
  32. int Andrew() {
  33. sort(p + 1, p + 1 + n);
  34. int len = 0;
  35. for (int i = 1; i <= n; ++i) {
  36. while (len > 1 && sgn(cross(stk[len] - stk[len - 1], p[i] - stk[len - 1])) == -1) {
  37. len--;
  38. }
  39. stk[++len] = p[i];
  40. }
  41. int k = len;
  42. for (int i = n - 1; i >= 1; --i) {
  43. while (len > k && sgn(cross(stk[len] - stk[len - 1], p[i] - stk[len - 1])) == -1) {
  44. len--;
  45. }
  46. stk[++len] = p[i];
  47. }
  48. return len;
  49. }
  50. double angle(Point p, Point q, Point s) {
  51. double x1 = q.x - p.x, y1 = q.y - p.y;
  52. double x2 = s.x - p.x, y2 = s.y - p.y;
  53. double ans = (x1 * x2 + y1 * y2) / (sqrt(x1 * x1 + y1 * y1) * sqrt(x2 * x2 + y2 * y2));
  54. return acos(ans);
  55. }
  56. int main() {
  57. int T;
  58. scanf("%d", &T);
  59. int kase = 0;
  60. while(T--) {
  61. map<pair<double, double>, int> mp;
  62. n = 0;
  63. int cnt;
  64. scanf("%d", &cnt);
  65. for (int i = 1; i <= cnt; ++i) {
  66. double x, y;
  67. scanf("%lf%lf", &x, &y);
  68. if(mp[make_pair(x, y)] == 0) {
  69. mp[make_pair(x, y)] = 1;
  70. p[++n].x = x;
  71. p[n].y = y;
  72. }
  73. }
  74. if(n < 3) {
  75. printf("Case %d: 0\n", ++kase);
  76. continue;
  77. }
  78. int t = Andrew();
  79. double min_angle = angle(stk[1], stk[t - 1], stk[2]);
  80. for (int i = 2; i < t; i++) {
  81. min_angle = min(min_angle, angle(stk[i], stk[i - 1], stk[i + 1]));
  82. }
  83. printf("Case %d: %.6lf\n", ++kase, min_angle * 180.0 / pi);
  84. }
  85. return 0;
  86. }

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. PAT 1051 Pop Sequence (25 分)

    返回 1051 Pop Sequence (25 分)   Given a stack which can keep M numbers at most. Push N numbers in the ...

  2. freemark 语法

    我们通过后端model. addAttribute() 传递到前端的值来进行界面渲染 它的循环语句 和其他的有点不同: if 循环 <#if 条件语句> </#if> if  ...

  3. rsync+inotify实现实时同步案例

    转自:http://chocolee.blog.51cto.com/8158455/1400596 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐 ...

  4. 7.Jmeter 快速入门教程--录制复杂web测试脚本

    Jmeter的功能简单,不需要有脚本语言的编写经验,纯图形界面添加测试场景, 用起来上手很快.但是如果手动添加每一个web(http/https)请求,费时又费力.而且有可能最后手动编写的和实际发的请 ...

  5. 转 loadrunner11 录制 chrome 浏览器

    chrome不设置代理的原始状态 图1 [LoadRunner]解决LR11无法录制Chrome浏览器脚本问题   LoadRunner录制脚本时,遇到高版本的IE.FireFox,或者Chrome浏 ...

  6. fixture实战---通过fixure,解决方法依赖逻辑

    import pytest@pytest.fixture()def login(): print('输入用户名密码登陆') def test_cart(login): print('用例1,登陆后执行 ...

  7. document.createDocumentFragment()的用法

    createDocumentFragment有什么作用呢? 调用多次document.body.append(),每次都要刷新页面一次.效率也就大打折扣了,而使用document_createDocu ...

  8. Python之列表转字典:setdefault、defaultdict、fromkeys

    setdefault result = {} data = [("p", 1), ("p", 2), ("p", 3), ("h& ...

  9. JavaScript_DOM详解

    节点操作: 查看对象属性的值obj.getAttribute() 如: //获取图片 var imgs = document.getElementsByTagName("img") ...

  10. Android开发:Handler的简单使用(一)

    1.Handler是什么? 原文: A Handler allows you to send and process Message and Runnable objects associated w ...