前言
初学半平面交,就遇到了这样巨大的坑,也是挺无语…
判断交集为空的情况
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.
显然是半平面交的板子题
这里我们不讲半平面交,我们只讲一些特例
下面有几个比较特殊的情况
1.向量共线
hack1:(存在向量相反且共线,且交集不是给出向量围成的多边形)
input:
3
9 8 9 0
3 3 3 4
3 2 1 7
output:
0.0
hack2:(存在向量相反且共线,且交集为空)
input:
2
1 1 2 2
2 2 1 1
output:
0.0
至少hack了我之前的那几份代码(逃
对于这种给出向量的题目,首先要注意加入构成外边界的4个向量
注意要逆时针加入(题目说了是向量左侧的平面)
我们寻求适用更为广泛的方法
对于方向相同的共线向量(下面说的极角均指atan2(y,x)
)
- 如果极角大于 0 0 0 小于 π \pi π ,就取较左的那个
- 如果极角小于 0 0 0 大于 − π -\pi −π ,就取较右的那个
- 如果极角等于 0 0 0 就取较上的那个
- 如果极角等于 π \pi π 就取较下的那个
这么一听好复杂哇,其实只要一个叉积判断下就好了
预处理一下这些烦人的共线向量,就可以放心halfplane()
了
具体看后面的代码,不急(逃
2.存在零向量
hack:(这种情况会卡极角排序)
input:
1
1 1 1 1
output:
100000000.0
这种情况一般题目会避免出现,但是不排除有的出题人搞了这种
其实这个情况容易判断,只要在读入的时候特判一下就好了
3.其他hack
目前没有发现什么其他hack,欢迎大家来hack我
4.AC代码
注意POJ的老古董编译器,输出 double
要用 printf()
+ %f
(居然不在题目上提醒)
这个出题人不写spj十分恶心人(无意冒犯),反正我搞了好久(6h+) 😓 😓 😓
代码如下
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;
#define int long long
// #define double long double
#define INF ((int)0x3f3f3f3f3f3f3f3f)
#define inf ((int)0xc0c0c0c0c0c0c0c0)
#define N (int)(1e5+15)
const double eps=1e-10;
struct vct{double x,y;}p[N];
#define pf(x) ((x)*(x))
vct operator+(vct a,vct b){return {a.x+b.x,a.y+b.y};}
vct operator-(vct a,vct b){return {a.x-b.x,a.y-b.y};}
vct operator*(vct a,double b){return {a.x*b,a.y*b};}
vct operator/(vct a,double b){return {a.x/b,a.y/b};}
double cross(vct a,vct b){return a.x*b.y-a.y*b.x;}
double dot(vct a,vct b){return a.x*b.x+a.y*b.y;}
double len(vct a){return sqrt(pf(a.x)+pf(a.y));}
struct line
{
vct p,way;
double k;
void mkline(vct a,vct b)
{
p=a;way=b;
k=atan2(b.y,b.x);
}
void mk(double x1,double y1,double x2,double y2)
{
mkline({x1,y1},{x2-x1,y2-y1});
}
}a[N];
int dcmp(double x)
{
if(fabs(x)<=eps)return 0;
return x>eps?1:-1;
}
bool operator<(line a,line b)
{
int d=dcmp(a.k-b.k);
if(d==0)return cross(b.p-a.p,b.way)>0;
return d<0;
}
bool onright(vct a,line b){return dcmp(cross(b.way,a-b.p))<0;}
vct intersect(line a,line b)
{
double x=cross(b.way,a.p-b.p)/cross(a.way,b.way);
return a.p+a.way*x;
}
int st,en;
line que[N];
bool halfplane(int n)
{
que[st=en=1]=a[1];
for(int i=2; i<=n; i++)
{
while(st<en&&onright(p[en],a[i]))--en;
while(st<en&&onright(p[st+1],a[i]))++st;
que[++en]=a[i];
if(st<en)p[en]=intersect(que[en-1],que[en]);
}
while(st<en&&onright(p[en],que[st]))--en;
if(en-st<=1)return 0;
p[st]=intersect(que[st],que[en]);
return 1;
}
double calc(vct *a,int n)
{
double res=0;
for(int i=1; i<n; i++)
res+=cross(a[i],a[i+1]);
res+=cross(a[n],a[1]);
if(fabs(res/=2)<=eps)res=0;
return res;
}
signed main()
{
int n,o=0,oo=0;
scanf("%lld",&n);
while(n--)
{
double x1,x2,y1,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
if(x1==x2&&y1==y2)continue;
a[++oo].mk(x1,y1,x2,y2);
}
a[++oo].mk(0,0,10000,0);
a[++oo].mk(10000,0,10000,10000);
a[++oo].mk(10000,10000,0,10000);
a[++oo].mk(0,10000,0,0);
sort(a+1,a+1+oo);
a[0].k=a[1].k-1;
for(int i=1; i<=oo; i++)
if(i==1||dcmp(a[i-1].k-a[i].k))
a[++o]=a[i];
if(!halfplane(o))puts("0.0");
else printf("%.1f\n",calc(p+st-1,en-st+1));
return 0;
}
总结
半平面交、旋转卡壳,这俩绝对是我目前见过最毒瘤的算法了 😅 😅 😅
啥?你说仙人掌?这不是毒瘤本瘤吗(逃
转载请说明出处