18.12.25 POJ 3525 Most Distant Point from the Sea(半平面+二分)

描述

The main land of Japan called Honshu is an island surrounded by the sea. In such an island, it is natural to ask a question: “Where is the most distant point from the sea?” The answer to this question for Honshu was found in 1996. The most distant point is located in former Usuda Town, Nagano Prefecture, whose distance from the sea is 114.86 km.

In this problem, you are asked to write a program which, given a map of an island, finds the most distant point from the sea in the island, and reports its distance from the sea. In order to simplify the problem, we only consider maps representable by convex polygons.

输入

The input consists of multiple datasets. Each dataset represents a map of an island, which is a convex polygon. The format of a dataset is as follows.

n    
x1   y1
   
xn   yn

Every input item in a dataset is a non-negative integer. Two input items in a line are separated by a space.

n in the first line is the number of vertices of the polygon, satisfying 3 ≤ n ≤ 100. Subsequent n lines are the x- and y-coordinates of the n vertices. Line segments (xiyi)–(xi+1yi+1) (1 ≤ i≤ n − 1) and the line segment (xnyn)–(x1y1) form the border of the polygon in counterclockwise order. That is, these line segments see the inside of the polygon in the left of their directions. All coordinate values are between 0 and 10000, inclusive.

You can assume that the polygon is simple, that is, its border never crosses or touches itself. As stated above, the given polygon is always a convex one.

The last dataset is followed by a line containing a single zero.

输出

For each dataset in the input, one line containing the distance of the most distant point from the sea should be output. An output line should not contain extra characters such as spaces. The answer should not have an error greater than 0.00001 (10−5). You may output any number of digits after the decimal point, provided that the above accuracy condition is satisfied.

样例输入

4
0 0
10000 0
10000 10000
0 10000
3
0 0
10000 0
7000 1000
6
0 40
100 20
250 40
250 70
100 90
0 70
3
0 0
10000 10000
5000 5001
0

样例输出

5000.000000
494.233641
34.542948
0.353553
  1 #include <iostream>
  2 #include <string.h>
  3 #include <algorithm>
  4 #include <stack>
  5 #include <string>
  6 #include <math.h>
  7 #include <queue>
  8 #include <stdio.h>
  9 #include <string.h>
 10 #include <set>
 11 #include <vector>
 12 #define maxn 105
 13 #define inf 999999
 14 #define EPS 1e-10
 15 using namespace std;
 16 
 17 int n;
 18 struct node {
 19     double x, y;
 20     node(){}
 21     node(double a, double b) :x(a), y(b) {}
 22 }v[maxn],nm[maxn],p[maxn];
 23 typedef node Vector;
 24 node operator + (node a, node b) {
 25     return node(a.x + b.x, a.y + b.y);
 26 }
 27 double operator *(node a, node b) {
 28     return a.x*b.x + a.y*b.y;
 29 }
 30 node operator -(node a, node b) {
 31     return node(a.x - b.x, a.y - b.y);
 32 }
 33 node operator *(double a, node b) {
 34     return node(a*b.x, a*b.y);
 35 }
 36 Vector operator / (Vector a, double b) { 
 37     return Vector(a.x / b, a.y / b); 
 38 }
 39 
 40 double det(node a, node b) {
 41     return a.x*b.y - a.y*b.x;
 42 }
 43 struct line {
 44     node p;
 45     Vector norm;
 46     double dire;
 47     line() {}
 48     line(node a, Vector v) :p(a), norm(v) { dire = atan2(v.y, v.x); }
 49 }q[maxn],L[maxn];
 50 bool operator <(line a, line b) {
 51     return a.dire < b.dire;
 52 }
 53 bool Onleft(node a, line b) {
 54     return det(a - b.p, b.norm) < EPS;
 55 }
 56 
 57 Vector normal(Vector a) {
 58     double len = sqrt(a*a);
 59     return Vector(-a.y / len, a.x / len);
 60 }
 61 
 62 Vector cross(line a, line b) {
 63     node p1 = a.p, p2 = a.p + a.norm, p3 = b.p, p4 = b.p + b.norm;
 64     double a1 = det(p3 - p1, p4 - p1), a2 = det(p4 - p2, p3 - p2);
 65     node x = a1*p2;
 66     return (a1*p2 + a2 * p1) / (a1 + a2);
 67 }
 68 bool Halfplane() {
 69     sort(L + 1, L + 1 + n);
 70     int head, tail;
 71     q[head = tail = 0] = L[1];
 72     for (int i = 2; i <= n; i++) {
 73         while (head < tail && !Onleft(p[tail - 1], L[i]))  tail--;
 74         while (head < tail && !Onleft(p[head], L[i]))  head++;
 75         q[++tail] = L[i];
 76         if (fabs(det(q[tail].norm, q[tail - 1].norm)) < EPS) {
 77             tail--;
 78             if (Onleft(L[i].p, q[tail]))  q[tail] = L[i];
 79         }
 80         if (head < tail)  
 81             p[tail - 1] = cross(q[tail], q[tail - 1]);
 82     }
 83     while (head < tail && !Onleft(p[tail - 1], q[head]))  tail--;
 84     return tail - head > 1;
 85 }
 86 void init() {
 87     for (int i = 1; i <= n; i++)
 88         scanf("%lf%lf", &v[i].x, &v[i].y);
 89     v[n + 1] = v[1];
 90     for (int i = 1; i <= n; i++)nm[i] = normal(v[i + 1] - v[i]);
 91     double left = 0, right = 10000;
 92     while (left + EPS < right) {
 93         double mid = (left + right) / 2;
 94         for (int i = 1; i <= n; i++)
 95             L[i] = line(v[i] + mid * nm[i], v[i + 1] - v[i]);
 96         if (Halfplane())
 97             left = mid;
 98         else
 99             right = mid;
100     }
101     printf("%.6lf\n", left);
102 }
103 
104 int main() {
105     while(scanf("%d",&n)&&n)
106         init();
107     return 0;
108 }
View Code

 

我连抄网上搜的板子都能抄错,把node类里面的构造函数参数类型写成了int,也就debug了一整天吧

 

其实WA点就上面这一个,但我眼瞎

posted @ 2018-12-25 16:37  TobicYAL  阅读(270)  评论(0编辑  收藏  举报