codeforce #329div2 B. Anton and Lines

B. Anton and Lines

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
The teacher gave Anton a large geometry homework, but he didn’t do it (as usual) as he participated in a regular round on Codeforces. In the task he was given a set of n lines defined by the equations y = ki·x + bi. It was necessary to determine whether there is at least one point of intersection of two of these lines, that lays strictly inside the strip between x1 < x2. In other words, is it true that there are 1 ≤ i < j ≤ n and x’, y’, such that:

y’ = ki * x’ + bi, that is, point (x’, y’) belongs to the line number i;
y’ = kj * x’ + bj, that is, point (x’, y’) belongs to the line number j;
x1 < x’ < x2, that is, point (x’, y’) lies inside the strip bounded by x1 < x2.
You can’t leave Anton in trouble, can you? Write a program that solves the given task.

Input
The first line of the input contains an integer n (2 ≤ n ≤ 100 000) — the number of lines in the task given to Anton. The second line contains integers x1 and x2 ( - 1 000 000 ≤ x1 < x2 ≤ 1 000 000) defining the strip inside which you need to find a point of intersection of at least two lines.

The following n lines contain integers ki, bi ( - 1 000 000 ≤ ki, bi ≤ 1 000 000) — the descriptions of the lines. It is guaranteed that all lines are pairwise distinct, that is, for any two i ≠ j it is true that either ki ≠ kj, or bi ≠ bj.

Output
Print “Yes” (without quotes), if there is at least one intersection of two distinct lines, located strictly inside the strip. Otherwise print “No” (without quotes).
Sample test(s)
input
4
1 2
1 2
1 0
0 1
0 2
output
NO
input
2
1 3
1 0
-1 3
output
YES
input
2
1 3
1 0
0 2
output
YES
input
2
1 3
1 0
0 3
output
NO
Note
In the first sample there are intersections located on the border of the strip, but there are no intersections located strictly inside it.
这里写图片描述

思路:
1 、先将式子进行推倒变化,
先将两条直线进行对比y =k1x+b1, y = k2x+b2
则化简得 x = (b1-b2)/(k2-k1)
x1 < x < x2
=>x1 < (b1-b2)/(k2-k1)< x2

x1 < (b1-b2)/(k2-k1)
=> (k2-k1)x1 > b1-b2
=>k2x1 +b2 > k1x1 +b1
x2 > (b1-b2)/(k2-k1)
=> (k2-k1)x2 < b1-b2
=>k2x2 +b2 < k1x2 +b1

即当两条直线存在k1x1+b1 < k2x1+b2 且 k1x2+b1 > k2x2+b2
或者k1x2+b1 < k2x2+b2 且 k1x1+b1 > k2x1+b2
就存在其交点x满足x1 < x < x2

2、用结构体存储每条直线的y1, y2

并将该结构体对y1进行排序,然后遍历该结构体的y2,遍历时还要注意结构体y1相等的情况。
再对i进行判断前,先搜索清所有与s[i].x1相等的情况记录个数p,然后挨个与max比较
若y2较小满足条件,flag =1;
最后比较完后,再遍历一次与max比较,存储在max内
i=i+p-1;
继续遍历。
注意再进行k[i]*x时有可能出现溢出现象,需强制转化

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
using namespace std;
#define cls(a, n) memset(a, n, sizeof(a))
#define REP(i, n) rep(i, 0, n)
#define inf 0x7ffffffff
#define rep(i, l, n) for (int i = l; i < n; i++)
typedef long long LL;
const int maxn = 100010;
int n, k[maxn], b[maxn], x2, x1;
struct sum{
    LL s1;
    LL s2;
}s[maxn];
int cmp(sum a,sum b) {
    return a.s1 < b.s1;
}
int main() {
    scanf("%d", &n);
    scanf("%d%d", &x1, &x2);
    rep(i, 0, n) {
        scanf("%d%d", &k[i], &b[i]);
        s[i].s1 = LL(k[i])*x1+b[i];
        s[i].s2 = LL(k[i])*x2+b[i];
    }
    sort(s, s+n,  cmp);
    int flag = 0;
    LL maxsum = -(LL)1<<60;
    rep(i, 0, n) {
        int p = i;
        while (p < n && s[p].s1 == s[i].s1) {
            if(s[p].s2<maxsum){
                flag=1;
            }
            p++;
        }
        rep(j, i, p) {
            maxsum = max(maxsum, s[j].s2);
        }
        i=p-1;
    }
    if (flag)
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值