CodeForces - 593B B - Anton and Lines

B - Anton and Lines

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 = k i·x + b i. 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 x 1 < x 2. In other words, is it true that there are 1 ≤ i < j ≤ n and x’, y’, such that:

y’ = k i * x’ + b i, that is, point (x’, y’) belongs to the line
number i; y’ = k j * x’ + b j, that is, point (x’, y’) belongs to the
line number j; x 1 < x’ < x 2, that is, point (x’, y’) lies inside the
strip bounded by x 1 < x 2. 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 x 1 and x 2 ( - 1 000 000 ≤ x 1 < x 2 ≤ 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 k i, b i ( - 1 000 000 ≤ k i, b i ≤ 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 k i ≠ k j, or b i ≠ b j.

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).

Examples
Input 4 1 2 1 2 1 0 0 1 0 2
Output NO
Input2 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
思路:看队里大佬写的博客才懂,其实挺水,后来想想,的确就那么做,对于数据范围比较大,我们可以想,先对一种情况排好序,然后去比较。具体想法:设左边x1处的纵坐标为a,则右边x2处的纵坐标为b,只要满足左边a1b2,就满足条件,所以先对一种即a排好序,然后只要出现b1>b2,就说明yes。

#include
using namespace std;
typedef long long ll;

struct node{
	ll a,b;
}ans[100001]; 

bool cmp(node l1,node l2){
	if(l1.a==l2.a) return l1.b<l2.b;
	return l1.a<l2.a;
}

int main()
{
	int flag=0;
	ll n,x1,x2;
	ll ki,bi;
	cin>>n>>x1>>x2;
	for(int i=0;i<n;i++)
	{
	cin>>ki>>bi;
    ans[i].a=ki*x1+bi;
    ans[i].b=ki*x2+bi;
	}
	sort(ans,ans+n,cmp);
	for(int i=0;i<n-1;i++)
	{
		if(ans[i].b>ans[i+1].b)
		{
			flag=1;
			break;
		}
	}
	if(flag) cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
	return 0;
}

你可能感兴趣的:(ACM)