LightOJ 1203 Guarding Bananas (凸包最小顶角)
题目链接: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}\) 的夹角:
\]
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 (凸包最小顶角)的更多相关文章
- Guarding Bananas
Guarding Bananas Once there was a lazy monkey in a forest. But he loved banana too much. One day the ...
- LightOj1203 - Guarding Bananas(凸包求多边形中的最小角)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1203 题意:给你一个点集,求凸包中最小的角:模板题,但是刚开始的时候模板带错了,错的我 ...
- LightOJ 1239 - Convex Fence 凸包周长
LINK 题意:类似POJ的宫殿围墙那道,只不过这道题数据稍微强了一点,有共线的情况 思路:求凸包周长加一个圆周长 /** @Date : 2017-07-20 15:46:44 * @FileNam ...
- LightOJ 1203--Guarding Bananas(二维凸包+内角计算)
1203 - Guarding Bananas PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 M ...
- kuangbin 带你飞 数学基础
模版整理: 晒素数 void init() { cas = ; ; i < MAXD ; i++) is_prime[i] = true; is_prime[] = is_prime[] = f ...
- (hdu step 7.1.5)Maple trees(凸包的最小半径寻找掩护轮)
称号: Maple trees Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- 【旋转卡壳+凸包】BZOJ1185:[HNOI2007]最小矩形覆盖
1185: [HNOI2007]最小矩形覆盖 Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 1945 Solve ...
- 【BZOJ1185】[HNOI2007]最小矩形覆盖(凸包,旋转卡壳)
[BZOJ1185][HNOI2007]最小矩形覆盖(凸包,旋转卡壳) 题面 BZOJ 洛谷 题解 最小的矩形一定存在一条边在凸包上,那么枚举这条边,我们还差三个点,即距离当前边的最远点,以及做这条边 ...
- [BZOJ1185][HNOI2007]最小矩形覆盖-[凸包+旋转卡壳]
Description 传送门 Solution 感性理解一下,最小矩形一定是由一条边和凸包上的边重合的. 然后它就是模板题了..然而真的好难调,小于大于动不动就打错. Code #include&l ...
随机推荐
- DCloud-Video:Html5 Video 实现方案
ylbtech-DCloud-Video:Html5 Video 实现方案 1.返回顶部 1.1. http://ask.dcloud.net.cn/article/569 1.2. 一. Html5 ...
- python模块学习之HTMLTestRunner模块生成HTML测试报告
#!/usr/bin/env python #-*- coding:utf-8 -*- from HTMLTestRunner import HTMLTestRunner import time im ...
- Java学习之面向对象特性-----封装
面向对象特性一.封装(Encapsulation)封装:是指隐藏对象的属性和实现细节,仅对外提供公共访问方式.好处: 将变化隔离 便于使用 提高复用性 提高安全性封装原则: 将不需要对外提供的内容都隐 ...
- day 53-1 Django基础三之视图函数
Django基础三之视图函数 本节目录 一 Django的视图函数view 二 CBV和FBV 三 使用Mixin 四 给视图加装饰器 五 Request对象 六 Response对象 一 Dja ...
- activiti7业务表示Businesskey
启动流程实例时,指定的businesskey,就会在act_ru_execution #流程实例的执行表中存储businesskey. Businesskey:业务标识,通常为业务表的主键,业务标识和 ...
- vue的品牌添加与筛选的功能集合
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- python 三方库字典
参考:https://github.com/jobbole/awesome-python-cn 环境管理 管理 Python 版本和环境的工具 p:非常简单的交互式 python 版本管理工具.官网 ...
- 【题解】Crossing River
题目描述 几个人过河,每次过两人一人回,速度由慢者决定,问过河所需最短时间. 输入格式 输入t组数据,每组数据第1行输入n,第2行输入n个数,表示每个人过河的时间. 输出格式 输出t行数据,每行1个数 ...
- Python面试题之“猴子补丁”(monkey patching)指的是什么?这种做法好吗?
“猴子补丁”就是指,在函数或对象已经定义之后,再去改变它们的行为. 举个例子: import datetime datetime.datetime.now = lambda: datetime.dat ...
- Electron 常见问题
导读: 以下记录了作者在实践中遇到的问题和最后的解决方法,如果有错误或者更新更完美的解决方案,欢迎留言指正.交流. 1.jQuery/RequireJS/Meteor/AngularJS 的问题 jQ ...