二分真是个神奇的东西,又是一个二分题目,而且这题分析的半平面交也比较巧妙,本人智商不够,二分和半平面交均想不到,
并且这题二分为什么会对,本渣渣也不太明白,留着慢慢的明白吧。
分析:
如果敌人只有一颗炸弹,你会把总部建在哪里呢?对于每个点,炸掉它以后不会暴露在外面的区域是一条有向直线的”左边“。这让我们想到了什么?
没错,半平面交!综合考虑所有点,所以建总部的范围就是所有这些半平面的交。
如果敌人有俩颗炸弹,总部应该建在哪里呢?分析后发现,敌人最聪明的做法是炸掉俩个连续的顶点,而不是分散火力(想一想,为什么)。这样,
每两个连续顶点对应一个新的半平面,可以建总部的范围仍然是所有半平面的交。
有了上面的分析,整个问题迎刃而解:二分答案,用上述方法判断大难是否可行(也就是半平面交是否为非空)。二分需要O(logn)时间,半平面交需要
O(nlogn)时间,总时间复杂度为O(n^2logn).
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cctype>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<stack>
using namespace std;
const double eps=1e-16;
const int maxn=50000+10;
int dcmp(double x)
{
if(fabs(x)<eps) return 0;
return x>0?1:-1;
//return fabs(x) < eps ? 0 : (x > 0 ? 1 : -1);
}
struct point
{
double x;
double y;
point(){}
point(double x,double y):x(x),y(y){}
void in()
{
cin>>x>>y;
}
void out()
{
cout<<x<<' '<<y<<endl;
}
point operator + (const point &t) const
{
return point(x+t.x,y+t.y);
}
point operator - (const point &t) const
{
return point(x-t.x,y-t.y);
}
point operator * (const double &t) const
{
return point(x*t,y*t);
}
};
double cross(point a,point b)
{
return a.x*b.y-a.y*b.x;
}
double dot(point a,point b)
{
return a.x*b.x+a.y*b.y;
}
double length(point a)
{
return sqrt(dot(a,a));
}
point nomal(point t)
{
double l=length(t);
return point(-t.y/l,t.x/l);
}
struct line
{
point p;
point 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(line l,point p)
{
return cross(l.v,p-l.p)>0;
}
point getintersection(line a,line b)
{
point u=a.p-b.p;
double t=cross(b.v,u)/cross(a.v,b.v);
return a.p+a.v*t;
}
point p[maxn];
line q[maxn];
point poly[maxn],pt[maxn];
line l[maxn];
int halfplaneintersection(line *l,int n,point *poly)
{
sort(l,l+n);
int first,last;
// point *p=new point[n];
// line *q=new line[n];
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]=getintersection(q[last-1],q[last]);
}
while(first<last && !onleft(q[first],p[last-1])) last--;
if(last-first<=1) return 0;
p[last]=getintersection(q[last],q[first]);
int m=0;
for(int i=first;i<=last;i++) poly[m++]=p[i];
return m;
}
int slove(int n)
{
if(n==3)
return 1;
int ll=1,r=n-3;
while(ll<=r)
{
int c=0;
int m=ll+r>>1;
for(int i=0;i<n;i++)
l[c++]=line(pt[(i+m+1)%n],pt[i]-pt[(i+m+1)%n]);
if(halfplaneintersection(l,c,poly))
ll=m+1;
else
r=m-1;
}
return ll;
}
int main()
{
int n;
while(cin>>n)
{
for(int i=0;i<n;i++)
pt[i].in();
cout<<slove(n)<<endl;
}
return 0;
}
本文深入探讨了如何利用二分查找技术和半平面交概念来解决特定问题。通过分析不同场景下总部选址的问题,阐述了在面对单一与双颗炸弹威胁时,如何运用几何原理与算法技巧进行最优布局。文章详细解释了二分查找的适用条件及其复杂度分析,并通过代码实例展示了实际应用过程。此外,还强调了理解半平面交对于问题解决的重要性,提供了一种直观且高效的方法来确定安全区域。
155

被折叠的 条评论
为什么被折叠?



