UVALive 3890 Most Distant Point from the Sea(凸包最大内接园)
一个n个点的凸多边形,求多边形中离多边形边界最远的距离。实际上就是求凸包最大内接圆的半径。
利用半平面交求解,每次二分枚举半径d,然后将凸包每条边所代表的半平面沿其垂直单位法向量平移d,看所有平移后的半平面的交集是否为空。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<bitset>
#include<vector>
#include<string>
#include<cstdio>
#include<cmath>
#include<stack>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define FF(i, a, b) for(int i=a; i<b; i++)
#define FD(i, a, b) for(int i=a; i>=b; i--)
#define REP(i, n) for(int i=0; i<n; i++)
#define CLR(a, b) memset(a, b, sizeof(a))
#define debug puts("**debug**")
#define LL long long
#define PB push_back
#define eps 1e-10
using namespace std; struct Point
{
double x, y;
Point (double x=0, double y=0):x(x), y(y) {}
};
typedef Point Vector; Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); } bool operator < (const Point& a, const Point& b)
{
return a.x < b.x || (a.x == b.x && a.y < b.y);
} int dcmp(double x)
{
if(fabs(x) < eps) return 0;
return x < 0 ? -1 : 1;
} bool operator == (const Point& a, const Point& b)
{
return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
} double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angel(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
double Area2(Vector A, Vector B, Vector C) { return Cross(B-A, C-A); }
Vector Normal(Vector a) //a向量的垂直法向量
{
return Vector(-a.y/Length(a), a.x/Length(a));
} struct Line
{
Point p;
Vector v;
double ang;
Line() {}
Line(Point p, Vector v): p(p), v(v) {ang = atan2(v.y, v.x); }
bool operator < (const Line& L) const
{
return ang < L.ang;
}
}; //点p在半平面的左边
bool onLeft(Line L, Point p) { return Cross(L.v, p-L.p) > 0; }
//直线交点
Point GetIntersection(Line a, Line b)
{
Vector u = a.p-b.p;
double t = Cross(b.v, u) / Cross(a.v, b.v);
return a.p + a.v*t;
} const int maxn = 200;
Point p[maxn], poly[maxn];
Line L[maxn];
Vector v[maxn], v2[maxn];
int n; //半平面交
Point pp[maxn];
Line qq[maxn];
int HalfplaneIntersection(Line* L, int n, Point* poly)
{
sort(L, L+n);
int first, last; qq[first=last=0] = L[0];
FF(i, 1, n)
{
while(first < last && !onLeft(L[i], pp[last-1])) last--;
while(first < last && !onLeft(L[i], pp[first])) first++;
qq[++last] = L[i];
if(fabs(Cross(qq[last].v, qq[last-1].v)) < eps)
{
last--;
if(onLeft(qq[last], L[i].p)) qq[last] = L[i];
}
if(first < last) pp[last-1] = GetIntersection(qq[last-1], qq[last]);
}
while(first < last && !onLeft(qq[first], pp[last-1])) last--;
if(last-first <= 1) return 0;
pp[last] = GetIntersection(qq[last], qq[first]); int m = 0;
FF(i, first, last+1) poly[m++] = pp[i];
return m;
} int main()
{
while(scanf("%d", &n), n)
{
REP(i, n) scanf("%lf%lf", &p[i].x, &p[i].y);
REP(i, n)
{
v[i] = p[(i+1)%n]-p[i];
v2[i] = Normal(v[i]);
}
double l=0, r=20000, mid;
while(r - l > eps)
{
mid = (l+r) / 2.0;
REP(i, n) L[i] = Line(p[i]+v2[i]*mid, v[i]);
int m = HalfplaneIntersection(L, n, poly);
if(!m) r=mid; else l=mid;
}
printf("%.6f\n", l);
}
return 0;
}
UVALive 3890 Most Distant Point from the Sea(凸包最大内接园)的更多相关文章
- uvalive 3890 Most Distant Point from the Sea
题意:求一个凸多边形中一点到边的最大距离. 思路:转换成在多边形内部,到每边距离为d的直线所围成的内多边形是否存在.也就是,二分距离+半平面交. #include<cstdio> #inc ...
- LA 3890 Most Distant Point from the Sea(半平面交)
Most Distant Point from the Sea [题目链接]Most Distant Point from the Sea [题目类型]半平面交 &题解: 蓝书279 二分答案 ...
- 简单几何(半平面交+二分) LA 3890 Most Distant Point from the Sea
题目传送门 题意:凸多边形的小岛在海里,问岛上的点到海最远的距离. 分析:训练指南P279,二分答案,然后整个多边形往内部收缩,如果半平面交非空,那么这些点构成半平面,存在满足的点. /******* ...
- UVA 3890 Most Distant Point from the Sea(二分法+半平面交)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11358 [思路] 二分法+半平面交 二分与海边的的距离,由法向量可 ...
- POJ 3525 Most Distant Point from the Sea [半平面交 二分]
Most Distant Point from the Sea Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5153 ...
- 【POJ】【3525】Most Distant Point from the Sea
二分+计算几何/半平面交 半平面交的学习戳这里:http://blog.csdn.net/accry/article/details/6070621 然而这题是要二分长度r……用每条直线的距离为r的平 ...
- POJ 3525 Most Distant Point from the Sea (半平面交+二分)
Most Distant Point from the Sea Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3476 ...
- POJ 3525/UVA 1396 Most Distant Point from the Sea(二分+半平面交)
Description The main land of Japan called Honshu is an island surrounded by the sea. In such an isla ...
- POJ3525-Most Distant Point from the Sea(二分+半平面交)
Most Distant Point from the Sea Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3955 ...
随机推荐
- Lucene.Net 2.3.1开发介绍 —— 四、搜索(一)
原文:Lucene.Net 2.3.1开发介绍 -- 四.搜索(一) 既然是内容筛选,或者说是搜索引擎,有索引,必然要有搜索.搜索虽然与索引有关,那也只是与索引后的文件有关,和索引的程序是无关的,因此 ...
- [读书笔记]设计原本[The Design of Design]
第1章 设计之命题 1.设计首先诞生于脑海里,再慢慢逐步成形(实现) 2.好的设计具有概念完整性:统一.经济.清晰.优雅.利落.漂亮... 第2章 工程师怎样进行设计思维——理性模型 1.有序模型的有 ...
- PHP从数据库获取的下拉树
<?php include "config.php"; include "mysql.php"; $db = new Mysql('test'); //几 ...
- 【android】下载文件至本应用程序的file文件夹或者sdcard
一.推断是否有sdcard卡 //推断是否有SD卡 //ture:有SD卡 //false:没有SD卡 public boolean avaiableMedia(){ String status ...
- mybatis 的简单使用
须要用到的包:(这里仅仅是当中一个版本号.其它的百度) mysql-connector-java-5.1.6-bin mybatis-3.2.2 先看项目文件夹: 配置文件mybatisconfig. ...
- NSHashTable 和 NSMapTable学习
今天,在实现play gif时间功能,我看见两个陌生班,只需看看这个纪录: NSSet和NSDictionary是两个经常使用的类,可是他们默认假定了当中对象的内存行为.对于NSSet.object是 ...
- matlab画棋盘格程序
转载请注明出处:zhouyelihua**http://blog.csdn.net/zhouyelihua/article/details/46674191** 意义 在摄像机标定过程中经常须要打印棋 ...
- 调用一个系统命令,并读取它的输出值(使用QProcess.readAll)
下面我们再看一个更复杂的例子,调用一个系统命令,这里我使用的是 Windows,因此需要调用 dir:如果你是在 Linux 进行编译,就需要改成 ls 了. mainwindow.h #ifndef ...
- 《转》Linux下的多线程编程
原地址:http://linux.chinaunix.net/doc/program/2001-08-11/642.shtml 1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程 ...
- [Android]图片资源管理学习
一.概念 几个概念:1.像素:像素是组成图像的最基本单位:点该点自身有大小,其中保存了颜色值 2.屏幕尺寸:screen size手机屏幕的物理尺寸.单位:inch(英寸)ex:4.0英寸 3.8英寸 ...