【几何】 Pair Of Lines CF contest961 D

给定n个在笛卡尔平面上的不重复整数坐标点,判断是否能画两条直线,使得每个点至少位于其中一条直线上。输入包含n个点的坐标,输出可能的情况或不可能的情况。
摘要由CSDN通过智能技术生成

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;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值