题意:半平面交求面积
CQOI 2006 凸多边形的弱化版。
好像做题的顺序搞错了。。
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 20005;
struct Point {
double x, y;
Point(){}
Point(double _x, double _y) : x(_x), y(_y) {}
Point operator +(const Point &b) const { return Point(x + b.x, y + b.y); }
Point operator -(const Point &b) const { return Point(x - b.x, y - b.y); }
double operator *(const Point &b) const { return x * b.y - y * b.x; }
Point operator *(double b) const { return Point(b * x, b * y); }
} poly[N];
struct Line {
Point x, v;
double ang;
Line(){}
Line(const Point &a, const Point &b) : x(a) {
v = b - a;
ang = atan2(v.y, v.x);
}
bool operator <(const Line &b) const {
if (ang == b.ang) return v * (b.x - x) < 0; // 极角相等时,这么算的叉积表示a在b的左边时返回Line a < b,unique的时候就会选择a而丢掉b。
return ang < b.ang;
}
bool operator ==(const Line &b) const { return ang == b.ang; }
Point intersection(const Line &b) {
Point u = x - b.x;
double t = (b.v * u) / (v * b.v); // 画了图才知道这么写的意思。。
return x + v * t;
}
} lines[N];
bool right(const Point &p, const Line &l) {
return l.v * (p - l.x) < 0;
}
int half_plane_intersection(Line l[], int n, Point poly[]) {
static Line q[N];
sort(l + 1, l + n + 1);
n = unique(l + 1, l + n + 1) - (l + 1); // 极角相等时,挑出最靠近“凸多边形中心”的那个,见Line::operator<。
int f = 1, r = 0, m = 0;
q[++r] = l[1]; q[++r] = l[2];
for (int i = 3; i <= n; i++) { // 逐个添加并删除半平面
while (f < r && right(q[r].intersection(q[r - 1]), l[i])) r--;
while (f < r && right(q[f].intersection(q[f + 1]), l[i])) f++;
q[++r] = l[i];
}
while (f < r && right(q[r].intersection(q[r - 1]), q[f])) r--; // 还要处理首尾
while (f < r && right(q[f].intersection(q[f + 1]), q[r])) f++;
q[r + 1] = q[f];
for (int i = f; i <= r; i++)
poly[++m] = q[i].intersection(q[i + 1]);
return m;
}
double calc_area(Point poly[], int m) {
double ans = 0;
if (m <= 2) return ans;
poly[m + 1] = poly[1];
for (int i = 1; i <= m; i++)
ans += poly[i] * poly[i + 1];
return fabs(ans) / 2;
}
int main() {
const int BORDER = 10000;
int n, j, line_num = 0;
double a, b, c, d;
scanf("%d", &n);
for (j = 1; j <= n; j++) {
scanf("%lf%lf%lf%lf", &a, &b, &c, &d);
lines[++line_num] = Line(Point(a, b), Point(c, d));
}
lines[++line_num] = Line(Point(0, 0), Point(BORDER, 0));
lines[++line_num] = Line(Point(BORDER, 0), Point(BORDER, BORDER));
lines[++line_num] = Line(Point(BORDER, BORDER), Point(0, BORDER));
lines[++line_num] = Line(Point(0, BORDER), Point(0, 0));
int m = half_plane_intersection(lines, line_num, poly);
printf("%.1lf", calc_area(poly, m));
return 0;
}
Uyuw's Concert
Time Limit: 6000MS | Memory Limit: 65536K | |
Total Submissions: 8557 | Accepted: 3221 |
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?
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.
![](https://i-blog.csdnimg.cn/blog_migrate/77b0c09a118290f4f1df02e3f0688d71.jpeg)
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.
![](https://i-blog.csdnimg.cn/blog_migrate/dae5cad522062dd46d442816aa5c2b43.jpeg)
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.