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?
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.
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.
5 0 0 0 1 1 1 1 -1 2 2
YES
5 0 0 1 0 2 1 1 1 2 3
NO
In the first example it is possible to draw two lines, the one containing the points 1, 3 and 5, and another one containing two remaining points.

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

本文探讨了一道算法题目,即如何通过给定点集确定这些点是否可以在两条直线上排列,并提供了一种有效的解决方案。文章详细介绍了使用向量叉乘进行三点共线判断的方法,并给出了完整的C++代码实现。

6万+

被折叠的 条评论
为什么被折叠?



