Newcoder 140 C.message(上凸包)

Description

给出 n n 条和y轴不平行的直线 y=aix+bi y = a i x + b i ,之后查询 m m 次,每次给出一条直线y=cix+di,查询这条直线与之前 n n 条直线在一四象限中交点横坐标的最大值,如果和之前n条直线在一四象限中没有交点则输出 No cross N o   c r o s s

Input

第一行一整数 n n ,之后n行每行输入两个整数 ai,bi a i , b i 表示一条直线,之后输入一整数 m m 表示查询数,每组查询输入两个整数ci,di表示一条直线

(1n,m5104,109ai,bi,ci,di109) ( 1 ≤ n , m ≤ 5 ⋅ 10 4 , − 10 9 ≤ a i , b i , c i , d i ≤ 10 9 )

Output

对于每组查询,如果所给直线和之前 n n 条直线在一四象限有交点则输出交点横坐标最大值,否则输出No cross

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 ,其交点横坐标为 dbac=dbca 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 的点的上凸包里或者出现在所有横坐标大于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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值