Description
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).
Example
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.
题意:
给你n条直线,问你在[x1, x2]的开区间里面存不存在至少一个交点。
思路:
根据直线的k 和 b求出与x = x1 和 x = x2的交点坐标,分别用b[].y1和b[].y2来记录。
这样只要存在i和j(i != j)满足b[i].y1 > b[j].y1 && b[i].y2 < b[j].y2就可以判定存在交点。(也就是假设有两条直线L1, L2,那么它们在一个区间内相交的条件就是,在区间左侧L1在L2上面,右侧L1在L2下面)
关键在于如何高效的查找。我们可以先按y1升序排列,再按y2升序排列,然后依次比较。
分析比较过程:(数组下标从1-n)首先取一个k = 1,然后遍历i(2 <= i <= n).
我们来比较b[k]和b[i]的y1, y2
(1) 满足上面的判定条件,存在交点;
(2) 剩下的情况,为了最大概率找到交点,我们取y2值较小的代替k向下比较,这样k = i;
这样查询只需要O(n)的时间复杂度。
注意:lv和rv会超int
ac代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct xx {
ll y1, y2;
}b[100005]; //直线与两边界的交点纵坐标
ll a[100005][2];//直线的两个参数
ll f(int i, ll x) {
return a[i][0] * x + a[i][1];
}
bool cmp(xx a, xx b)
{
if(a.y1 != b.y1)
return a.y1 > b.y1;
else
return a.y2 > b.y2;
}
int main()
{
int n; cin >> n;
ll x1, x2;
cin >> x1 >> x2;
for (int i = 0; i < n; ++i)
{
cin >> a[i][0] >> a[i][1];
b[i].y1 = f(i, x1);
b[i].y2 = f(i, x2);
}
sort(b, b+n, cmp);
int flag = 0;
for (int i = 0, j = 1; i < n && j < n; ++j)
{
if(b[i].y1 > b[j].y1 && b[i].y2 < b[j].y2 && i!=j)
{
flag = 1;
break;
}
else
i = j;
}
if(flag)
printf("YES\n");
else
printf("NO\n");
}