Description
给出 n n 条和轴不平行的直线 y=aix+bi y = a i x + b i ,之后查询 m m 次,每次给出一条直线,查询这条直线与之前 n n 条直线在一四象限中交点横坐标的最大值,如果和之前条直线在一四象限中没有交点则输出 No cross N o c r o s s
Input
第一行一整数 n n ,之后行每行输入两个整数 ai,bi a i , b i 表示一条直线,之后输入一整数 m m 表示查询数,每组查询输入两个整数表示一条直线
(1≤n,m≤5⋅104,−109≤ai,bi,ci,di≤109) ( 1 ≤ n , m ≤ 5 ⋅ 10 4 , − 10 9 ≤ a i , b i , c i , d i ≤ 10 9 )
Output
对于每组查询,如果所给直线和之前 n n 条直线在一四象限有交点则输出交点横坐标最大值,否则输出
Sample Input
2
0 -1
1 2
3
-1 4
2 -2
2 5
Sample Output
5.000000000000000
4.000000000000000
No cross
Solution
对于直线 y=ax+b y = a x + b 和直线 y=cx+d y = c x + d ,其交点横坐标为 d−ba−c=−d−bc−a d − b a − c = − d − b c − a ,即为点 (a,b) ( a , b ) 和点 (c,d) ( c , d ) 斜率的相反数,那么对于一组查询 (c,d) ( c , d ) ,即求 (c,d) ( c , d ) 到所有点 (ai,bi) ( a i , b i ) 的斜率最小值,显然该斜率最小值出现在所有横坐标小于 c c 的点的上凸包里或者出现在所有横坐标大于的点的下凸包里,对这两者二分取到最小值之后更新答案即可
Code
#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn 100005
#define eps 1e-6
struct Point
{
double x,y;
int id;
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;
}
bool operator<(const Point &b)const
{
if(x!=b.x)return x<b.x;
return y<b.y;
}
}p[maxn],s[maxn];
double k(Point a,Point b)
{
return (b.y-a.y)/(b.x-a.x);
}
int n,m;
double ans[maxn];
void convex_hull(int n)
{
int top=0;
for(int i=1;i<=n;i++)
if(p[i].id)
{
if(top==0)continue;
int l=0,r=top,mid;
while(r-l>1)
{
mid=(l+r)/2;
if(k(p[i],s[mid-1])<k(p[i],s[mid]))r=mid;
else l=mid;
}
ans[p[i].id]=max(ans[p[i].id],-k(p[i],s[l]));
}
else
{
while(top>1&&((s[top-1]-s[top-2])^(p[i]-s[top-1]))>=0)top--;
s[top++]=p[i];
}
}
void Solve(int n)
{
sort(p+1,p+n+1);
convex_hull(n);
reverse(p+1,p+n+1);
convex_hull(n);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%lf%lf",&p[i].x,&p[i].y),p[i].id=0;
scanf("%d",&m);
for(int i=1;i<=m;i++)scanf("%lf%lf",&p[i+n].x,&p[i+n].y),p[i+n].id=i;
Solve(n+m);
for(int i=1;i<=m;i++)
if(ans[i]<eps)printf("No cross\n");
else printf("%.6f\n",ans[i]);
return 0;
}