961D - Pair Of Lines

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.


题的大意是,给出N个点,看这两个点是否在两条线上。
有个关键点要清楚,那就是:刚开始的三个点,组成的三个线很重要,最后所有点在的两条线,其中一条或两条肯定是之前那三条里。
判断三个点是够共线的方法,是用向量叉乘,二维形式的;
附代码:

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

struct point
{
	long long x;
	long long y;
	point (long long _x = 0, long long _y = 0) : x(_x), y(_y) {}
};

int t;
vector<point> po;

bool one_line(point a, point b, point c)
{
	point p(b.x - a.x, b.y - a.y);
	point q(c.x - b.x, c.y - b.y);
	if (!(p.x * q.y - q.x * p.y))
		return true;
	return false;
}
bool solve(int a, int b)
{
	vector<point> nl;
	for (int i = 0; i < t; i++)
	{
		if (i == a || i == b)
			continue;
		if (!one_line(po[a], po[b], po[i]))
			nl.push_back(po[i]);
	}
	if (nl.size() < 2)
		return true;
	for (int i = 2; i < nl.size(); i++)
		if (!one_line(nl[0], nl[1], nl[i]))
			return false;
	return true;
}
int main()
{
#ifdef LOCAL
	freopen("C:/input.txt", "r", stdin);
#endif
	cin >> t;
	for (int ti = 0; ti < t; ti++)
	{
		int _x, _y;
		scanf("%d%d", &_x, &_y);
		po.push_back(point(_x, _y));
	}
	if (t <= 4 || solve(0, 1) || solve(0, 2) || solve(1, 2))
		cout << "YES" << endl;
	else
		cout << "NO" << endl;

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值