【几何】 Pair Of Lines CF contest961 D

D. Pair Of Lines
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given n points on Cartesian plane. Every point is a lattice point (i. e. both of its coordinates are integers), and all points are distinct.

You may draw two straight lines (not necessarily distinct). Is it possible to do this in such a way that every point lies on at least one of these lines?

Input

The first line contains one integer n (1 ≤ n ≤ 105) — the number of points you are given.

Then n lines follow, each line containing two integers xi and yi (|xi|, |yi| ≤ 109)— coordinates of i-th point. All n points are distinct.

Output

If it is possible to draw two straight lines in such a way that each of given points belongs to at least one of these lines, print YES. Otherwise, print NO.

Examples
input
Copy
5
0 0
0 1
1 1
1 -1
2 2
output
Copy
YES
input
Copy
5
0 0
1 0
2 1
1 1
2 3
output
Copy
NO
Note

In the first example it is possible to draw two lines, the one containing the points 13 and 5, and another one containing two remaining points.


// 取前三个点 若存在这样两条线 其中两点间连线必为两条之一
// 不在这条直线上的点是否在同一直线上

#include <bits/stdc++.h>
using namespace std;

const int mn = 100010;

long long x[mn], y[mn];

int main()
{
	int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
		scanf("%lld %lld", &x[i], &y[i]);

	if (n <= 4)
	{
		printf("YES\n");
		return 0;
	}

	bool flag = 0;
	for (int i = 1; i <= 2; i++)
	{
		for (int j = i + 1; j <= 3; j++)
		{
            long long a = y[j] - y[i];
            long long b = x[i] - x[j];
            long long c = y[i] * x[j] - y[j] * x[i];
            long long xa , ya, xb, yb;
            int t = 0;
            int k = 1;
            for (k = 1; k <= n; k++)
			{
				if (a * x[k] + b * y[k] + c == 0)
					continue;
				else
				{
					t ++;  // 不在这条直线上点的数量
					if (t == 1)
						xa = x[k], ya = y[k];
					else if(t == 2)
						xb = x[k], yb = y[k];
					else
						break;
				}
			}
			if (t <= 2)
			{
				flag = 1;
				break;
			}

			long long ta = yb - ya;
            long long tb = xa - xb;
            long long tc = ya * xb - yb * xa;
            bool q = 0; // 是否有不在两条直线上的
            for (int p = k; p <= n; p++)
			{
				if ((a * x[p] + b * y[p] + c == 0) || ta * x[p] + tb * y[p] + tc == 0)
					continue;
				else
					q = 1;
				if (q)
					break;
			}
			if (!q)
			{
				flag = 1;
				break;
			}
		}
		if (flag)
			break;
	}
	if (!flag)
		printf("NO\n");
	else
		printf("YES\n");
  return 0;
}





猜你喜欢

转载自blog.csdn.net/ummmmm/article/details/80939111
今日推荐