UVA 3890 Most Distant Point from the Sea(二分法+半平面交)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11358

【思路】

二分法+半平面交

二分与海边的的距离,由法向量可以得到平移后的各边,半平面交在特定精度判断是否有交集。

【代码】

 1 #include<cmath>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 const double eps = 1e-7;
 8
 9 struct Pt {
10     double x,y;
11     Pt(double x=0,double y=0):x(x),y(y) {}
12 };
13 typedef Pt vec;
14 struct Line {
15     Pt P; vec v;
16     double ang;
17     Line () {};
18     Line (Pt P,vec v):P(P),v(v) { ang=atan2(v.y , v.x); }
19     bool operator < (const Line& rhs) const{
20         return ang<rhs.ang;
21     }
22 };
23
24 vec operator - (Pt A,Pt B) { return vec(A.x-B.x,A.y-B.y); }
25 vec operator + (vec A,vec B) { return vec(A.x+B.x,A.y+B.y); }
26 vec operator * (vec A,double p) { return vec(A.x*p,A.y*p); }
27 double Dot(vec A,vec B) { return A.x*B.x+A.y*B.y; }
28 double cross(Pt A,Pt B) { return A.x*B.y-A.y*B.x; }
29 double Len(vec A) { return sqrt(Dot(A,A)); }
30 vec Normal(vec A) { double L=Len(A); return vec(-A.y/L,A.x/L); }
31
32 bool onleft(Line L,Pt P) { return cross(L.v,P-L.P)>0; }
33
34 Pt LineIntersection(Line a,Line b) {
35     vec u=a.P-b.P;
36     double t=cross(b.v,u)/cross(a.v,b.v);
37     return a.P+a.v*t;
38 }
39 int HalfplaneIntersection(Line* L,int n,Pt* poly) {
40     sort(L,L+n);
41     int first,last;
42     Pt *p=new Pt[n];
43     Line *q=new Line[n];
44     q[first=last=0]=L[0];
45     for(int i=1;i<n;i++) {
46         while(first<last && !onleft(L[i],p[last-1])) last--;
47         while(first<last && !onleft(L[i],p[first])) first++;
48         q[++last]=L[i];
49         if(fabs(cross(q[last].v,q[last-1].v))<eps) {
50             last--;
51             if(onleft(q[last],L[i].P)) q[last]=L[i];
52         }
53         if(first<last) p[last-1]=LineIntersection(q[last-1],q[last]);
54     }
55     while(first<last && !onleft(q[first],p[last-1])) last--;
56     if(last-first<=1) return 0;
57     p[last]=LineIntersection(q[last],q[first]);
58     int m=0;
59     for(int i=first;i<=last;i++) poly[m++]=p[i];
60     return m;
61 }
62
63 const int N = 200+10;
64 Pt p[N],poly[N];
65 Line L[N];
66 vec v[N] , v2[N];
67 int n;
68
69 int main() {
70     while(scanf("%d",&n)==1 && n) {
71         int m,x,y;
72         for(int i=0;i<n;i++) {
73             scanf("%d%d",&x,&y);
74             p[i]=Pt(x,y);
75         }
76         for(int i=0;i<n;i++) {
77             v[i]=p[(i+1)%n]-p[i];
78             v2[i]=Normal(v[i]);
79         }
80         double left=0 , right=20000;
81         while(right-left>eps) {
82             double mid=left+(right-left)/2;
83             for(int i=0;i<n;i++) L[i]=Line(p[i]+v2[i]*mid,v[i]);
84             m=HalfplaneIntersection(L,n,poly);
85             if(!m) right=mid; else left=mid;
86         }
87         printf("%.6lf\n",left);
88     }
89     return 0;
90 }
时间: 2024-10-16 19:52:28

UVA 3890 Most Distant Point from the Sea(二分法+半平面交)的相关文章

poj3525Most Distant Point from the Sea(半平面交)

链接 求凸多边形内一点距离边最远. 做法:二分+半平面交判定. 二分距离,每次让每条边向内推进d,用半平面交判定一下是否有核. 本想自己写一个向内推进..仔细一看发现自己的平面交模板上自带.. 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector>

POJ 3525 Most Distant Point from the Sea (半平面交向内推进+二分半径)

题目链接 题意 : 给你一个多边形,问你里边能够盛的下的最大的圆的半径是多少. 思路 :先二分半径r,半平面交向内推进r.模板题 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <math.h> 5 const double eps = 1e-10 ; 6 7 using namespace std ; 8 9 struct node 10 { 11 do

uvalive 3890 Most Distant Point from the Sea

题意:求一个凸多边形中一点到边的最大距离. 思路:转换成在多边形内部,到每边距离为d的直线所围成的内多边形是否存在.也就是,二分距离+半平面交. 1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 #include<algorithm> 5 #include<iostream> 6 #include<memory.h> 7 #include<cstdlib>

简单几何(半平面交+二分) LA 3890 Most Distant Point from the Sea

题目传送门 题意:凸多边形的小岛在海里,问岛上的点到海最远的距离. 分析:训练指南P279,二分答案,然后整个多边形往内部收缩,如果半平面交非空,那么这些点构成半平面,存在满足的点. /************************************************ * Author :Running_Time * Created Time :2015/11/10 星期二 14:16:17 * File Name :LA_3890.cpp ********************

POJ3525-Most Distant Point from the Sea(二分+半平面交)

Most Distant Point from the Sea Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3955   Accepted: 1847   Special Judge Description The main land of Japan called Honshu is an island surrounded by the sea. In such an island, it is natural t

poj 3525 Most Distant Point from the Sea 半平面交 + 二分

题目来源: http://poj.org/problem?id=3525 分析: 题意:给定一个凸多边形,求多边形中距离边界最远的点到边界的距离. 思路 : 每次将凸多边形每条边往里平移d,判断是否存在核:二分d即可. 多边形边上的点(x , y)往里平移d 后的 坐标: s , e  为向量的 起点和终点, len 为起点和终点的距离, h 为平移的距离 x' = x + dx y' = y + dy dx = ( s.y - e.y ) / len * h ,( 原理 是 利用 三角形的相似

Poj 3525 Most Distant Point from the Sea

地址:http://poj.org/problem?id=3525 题目: Most Distant Point from the Sea Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5167   Accepted: 2331   Special Judge Description The main land of Japan called Honshu is an island surrounded by the s

LA 3890 (半平面交) Most Distant Point from the Sea

题意: 给出一个凸n边形,求多边形内部一点使得该点到边的最小距离最大. 分析: 最小值最大可以用二分. 多边形每条边的左边是一个半平面,将这n个半平面向左移动距离x,则将这个凸多边形缩小了.如果这n个半平面交非空,则存在这样距离为x的点,反之则不存在. 半平面交的代码还没有完全理解. 和凸包类似,先对这些半平面进行极角排序.每次新加入的平面可能让队尾的平面变得“无用”,从而需要删除.由于极角序是环形的,所以也可能让队首元素变得“无用”.所以用双端队列来维护. 1 //#define LOCAL

UVA The most distant state

题目如下: Problem A The Most Distant State Input: standard input Output: standard output The 8-puzzle is a square tray inwhich eight square tiles are placed. The remaining ninth square is uncovered.Each tile has a number on it. A tile that is adjacent to