题目链接:http://poj.org/problem?id=2451
Time Limit: 6000MS Memory Limit: 65536K
Description
Prince Remmarguts solved the CHESS puzzle successfully. As an award, Uyuw planned to hold a concert in a huge piazza named after its great designer Ihsnayish.
The piazza in UDF - United Delta of Freedom’s downtown was a square of [0, 10000] * [0, 10000]. Some basket chairs had been standing there for years, but in a terrible mess. Look at the following graph.
In this case we have three chairs, and the audiences face the direction as what arrows have pointed out. The chairs were old-aged and too heavy to be moved. Princess Remmarguts told the piazza's current owner Mr. UW, to build a large stage inside it. The stage must be as large as possible, but he should also make sure the audience in every position of every chair would be able to see the stage without turning aside (that means the stage is in the forward direction of their own).
To make it simple, the stage could be set highly enough to make sure even thousands of chairs were in front of you, as long as you were facing the stage, you would be able to see the singer / pianist – Uyuw.
Being a mad idolater, can you tell them the maximal size of the stage?
Input
In the first line, there's a single non-negative integer N (N <= 20000), denoting the number of basket chairs. Each of the following lines contains four floating numbers x1, y1, x2, y2, which means there’s a basket chair on the line segment of (x1, y1) – (x2, y2), and facing to its LEFT (That a point (x, y) is at the LEFT side of this segment means that (x – x1) * (y – y2) – (x – x2) * (y – y1) >= 0).
Output
Output a single floating number, rounded to 1 digit after the decimal point. This is the maximal area of the stage.
Sample Input
3
10000 10000 0 5000
10000 5000 5000 10000
0 5000 5000 0
Sample Output
54166666.7
Hint
Sample input is the same as the graph above, while the correct solution for it is as below:
I suggest that you use Extended in pascal and long double in C / C++ to avoid precision error. But the standard program only uses double.
Problem solving report:
Description: 给出一些向量,求出围成的多边形的核的面积。
Problem solving: 半平面交板子,在四周加上一个边界就行了。
Accepted Code:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 20550;
const double eps = 1e-16;
typedef struct point {
double x, y;
}vect;
struct line {
vect p;
point u, v;
bool operator < (const line& s) const {
return atan2(s.p.y, s.p.x) > atan2(p.y, p.x);
}
};
vect operator - (const point a, const point b) {
vect p;
p.x = a.x - b.x;
p.y = a.y - b.y;
return p;
}
double dist(const point a, const point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
int pnz(double x) {
return x < -eps ? -1 : x > eps ? 1 : 0;
}
double Cross(const vect a, const vect b) {
return a.x * b.y - a.y * b.x;
}
int Onleft(const point p, const line l) {
return pnz(Cross(l.p, p - l.u));
}
point Inter(const line a, const line b) {
point p;
double t = Cross(b.p, a.u - b.u) / Cross(a.p, b.p);
p.x = a.u.x + t * a.p.x;
p.y = a.u.y + t * a.p.y;
return p;
}
double Area(const point p[], int n) {
double area = 0;
for (int i = 1; i < n - 1; i++)
area += Cross(p[i] - p[0], p[i + 1] - p[0]);
return area / 2;
}
double Halfplane(line Sline[], int n) {
point Spt[MAXN];
int Q[MAXN] = {0};
int l = 0, r = 0;
sort(Sline, Sline + n);
for (int i = 1; i < n; i++) {
while (l < r && Onleft(Spt[r - 1], Sline[i]) < 0)
r--;
while (l < r && Onleft(Spt[l], Sline[i]) < 0)
l++;
Q[++r] = i;
if (!pnz(Cross(Sline[i].p, Sline[Q[r - 1]].p))) {
r--;
if (Onleft(Sline[i].u, Sline[Q[r]]) > 0)
Q[r] = i;
}
else Spt[r - 1] = Inter(Sline[i], Sline[Q[r - 1]]);
}
while (l < r && Onleft(Spt[r - 1], Sline[Q[l]]) < 0)
r--;
while (l < r && Onleft(Spt[l], Sline[Q[r]]) < 0)
l++;
if (r - l < 2)
return 0;
Spt[r] = Inter(Sline[Q[l]], Sline[Q[r]]);
return Area(Spt + l, r - l + 1);
}
int main() {
int n, t;
line Sl[MAXN], Sline[MAXN];
point s, e, p[5];
p[0].x = 0, p[0].y = 0;
p[1].x = 10000, p[1].y = 0;
p[2].x = 10000, p[2].y = 10000;
p[3].x = 0, p[3].y = 10000;
while (~scanf("%d", &n)) {
p[4] = p[0];
for (int i = 0; i < 4; i++) {
Sl[i].u = p[i];
Sl[i].v = p[i + 1];
Sl[i].p = p[i + 1] - p[i];
}
for (int i = 4; i < n + 4; i++) {
scanf("%lf%lf%lf%lf", &s.x, &s.y, &e.x, &e.y);
Sl[i].u = s;
Sl[i].v = e;
Sl[i].p = e - s;
}
printf("%.1f\n", Halfplane(Sl, n + 4));
}
return 0;
}