POJ 2451 nlog(n)半平面交裸题。

来源:互联网 发布:矩阵行列式定义 编辑:程序博客网 时间:2024/05/20 21:49
Uyuw's Concert
Time Limit: 6000MS Memory Limit: 65536KTotal Submissions: 6587 Accepted: 2611

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

310000 10000 0 500010000 5000 5000 100000 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.

题意:一个区域内,给定若干半平面,求最终围成的区域的面积,

由于数据较大,因此适合nlogn的半平面交,

方法如下:

step1. 将所有半平面按极角排序,对于极角相同的,选择性的保留一个。 O(nlogn)
step2. 使用一个双端队列(deque),加入最开始2个半平面。
step3. 每次考虑一个新的半平面:
a.while deque顶端的两个半平面的交点在当前半平面外:删除deque顶端的半平面
b.while deque底部的两个半平面的交点在当前半平面外:删除deque底部的半平面
c.将新半平面加入deque顶端
step4.删除两端多余的半平面。
具体方法是:
a.while deque顶端的两个半平面的交点在底部半平面外:删除deque顶端的半平面
b.while deque底部的两个半平面的交点在顶端半平面外:删除deque底部的半平面
重复a,b直到不能删除为止。
step5:计算出deque顶端和底部的交点即可。

模拟了半天,有点理解了,

代码:

/* ***********************************************Author :_rabbitCreated Time :2014/5/4 15:03:55File Name :20.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <iostream>#include <algorithm>#include <sstream>#include <stdlib.h>#include <string.h>#include <limits.h>#include <string>#include <time.h>#include <math.h>#include <queue>#include <stack>#include <set>#include <map>using namespace std;#define INF 0x3f3f3f3f#define eps 1e-8#define pi acos(-1.0)typedef long long ll;int dcmp(double x){if(fabs(x)<eps)return 0;return x>0?1:-1;}struct Point{double x,y;Point(double _x=0,double _y=0){x=_x;y=_y;}};Point operator + (const Point &a,const Point &b){return Point(a.x+b.x,a.y+b.y);}Point operator - (const Point &a,const Point &b){return Point(a.x-b.x,a.y-b.y);}Point operator * (const Point &a,const double &p){return Point(a.x*p,a.y*p);}Point operator / (const Point &a,const double &p){return Point(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);}bool operator == (const Point &a,const Point &b){return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;}double Dot(Point  a,Point b){return a.x*b.x+a.y*b.y;}double Length(Point a){return sqrt(Dot(a,a));}double Angle(Point a,Point b){return acos(Dot(a,b)/Length(a)/Length(b));}double angle(Point a){return atan2(a.y,a.x);}double Cross(Point a,Point b){return a.x*b.y-a.y*b.x;}Point vecunit(Point a){return a/Length(a);}Point Normal(Point a){return Point(-a.y,a.x)/Length(a);}Point Rotate(Point a,double rad){return Point(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));}double Area2(Point a,Point b,Point c){return Length(Cross(b-a,c-a));}struct Line{Point p,v;double ang;Line(){};Line(Point p,Point v):p(p),v(v){ang=atan2(v.y,v.x);}bool operator < (const Line &L) const {return ang<L.ang;}};bool OnLeft(const Line &L,const Point &p){return Cross(L.v,p-L.p)>0;}Point GetLineIntersection(Point p,Point v,Point q,Point w){Point u=p-q;double t=Cross(w,u)/Cross(v,w);return p+v*t;}Point GetLineIntersection(Line a,Line b){return GetLineIntersection(a.p,a.v,b.p,b.v);}vector<Point> HPI(vector<Line> L){int n=L.size();sort(L.begin(),L.end());//将所有半平面按照极角排序。int first,last;vector<Point> p(n);vector<Line> q(n);vector<Point> ans;q[first=last=0]=L[0];for(int i=1;i<n;i++){while(first<last&&!OnLeft(L[i],p[last-1]))last--;//删除顶部的半平面while(first<last&&!OnLeft(L[i],p[first]))first++;//删除底部的半平面q[++last]=L[i];//将当前的半平面假如双端队列顶部。if(fabs(Cross(q[last].v,q[last-1].v))<eps){//对于极角相同的,选择性保留一个。last--;if(OnLeft(q[last],L[i].p))q[last]=L[i];}if(first<last)p[last-1]=GetLineIntersection(q[last-1],q[last]);//计算队列顶部半平面交点。}while(first<last&&!OnLeft(q[first],p[last-1]))last--;//删除队列顶部的无用半平面。if(last-first<=1)return ans;//半平面退化p[last]=GetLineIntersection(q[last],q[first]);//计算队列顶部与首部的交点。for(int i=first;i<=last;i++)ans.push_back(p[i]);//将队列中的点复制。return ans;}double PolyArea(vector<Point> p){int n=p.size();double ans=0;for(int i=1;i<n-1;i++)ans+=Cross(p[i]-p[0],p[i+1]-p[0]);return fabs(ans)/2;}int main(){     //freopen("data.in","r",stdin);     //freopen("data.out","w",stdout);     int n; while(~scanf("%d",&n)){ Point a,b; vector<Line> L; Line s; a=Point(0,0);b=Point(10000,0);s=Line(a,b-a);L.push_back(s); a=Point(10000,0);b=Point(10000,10000);s=Line(a,b-a);L.push_back(s); a=Point(10000,10000);b=Point(0,10000);s=Line(a,b-a);L.push_back(s); a=Point(0,10000);b=Point(0,0);s=Line(a,b-a);L.push_back(s); while(n--){ scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y); L.push_back(Line(a,b-a)); } vector<Point> ans=HPI(L); double out=PolyArea(ans); printf("%.1f\n",out); }     return 0;}


0 0
原创粉丝点击