codeforces 593B Anton and Lines

来源:互联网 发布:火炮兰灵族捏脸数据 编辑:程序博客网 时间:2024/03/29 18:13

新博客地址
题意:给出n条直线(1<=n<=1e5),问你在(x1,x2)这个区间是否存在交点

当n = 1

明显没有交点,NO

当n = 2: 如下图

这里写图片描述

其中a1为直线L1与x1的交点,b1为直线L1与x2的交点,可以发现如果两条直线要
在(x1,x2)之间相交,那么必须满足a1

当n = 3:

这里写图片描述

我们发现如果L3与L1在(x1,x2)有交点,那么L3与L2在(x1,x2)必有交点,所以
所以,这种情况,我们只需要判断L2是否与L1有交点,L3是否与L2有交点,就
可以判断出这3条线在(x1,x2)之间是否有交点。

当n = 4,5,6…..

当我们按照所有的ai从小到大排序,那么很明显当存在L[i]与L[i-1]有交点,那么就可以
说明所有的直线在(x1,x2)之间存在交点。那么如果所有的L[i]与L[i-1]都不存在交点,是否
可以说明所有的直线在(x1,x2)之间不存在交点,不一定,看这种情况:

这里写图片描述

可以发现当a2=a3的时候l3与l2没有交点,但是l3与l1却有交点,这种情况的时候,我们发现,
如果把L3排在L2前面的话,就可以满足规律了,所以我们按照ai从小到达排,当ai相等,按照
bi从小到大排,然后判断是否存在a[i] < a[i+1]并且b[i] > b[i+1]即可。

代码:

#include <cstdio>#include <cstring>#include <cstdlib>#include <ctime>#include <cmath>#include <cstdlib>#include <algorithm>using namespace std;typedef long long Long;const int maxn = 1e5 + 10;class line{public:    line(int a = 0, int b = 0) : a(a),b(b) {}    bool operator < (const line& r) const    {        return a < r.a || (a==r.a && b<r.b);    }    bool intersect(const line& r) const    {        return a > r.a && b < r.b;    }    void read(int x1, int x2)    {        int t1, t2; scanf("%d%d", &t1, &t2);        a = 1ll*t1*x1+t2; b = 1ll*t1*x2+t2;    }private:    Long a, b;} ln[maxn];int main () {        int n, x1, x2; scanf("%d%d%d", &n, &x1, &x2);    bool tag = false;    for (int i = 0; i < n; i++)    {        ln[i].read(x1, x2);    }    sort(ln, ln+n);    for (int i = 1; i < n; i++)    {        if (ln[i].intersect(ln[i-1]))        {            tag = true;        }    }    if (tag)    {        puts("YES");    }    else puts("NO");    return 0;}
0 0