D. Pair Of Lines
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
inputCopy
5
0 0
0 1
1 1
1 -1
2 2
outputCopy
YES
inputCopy
5
0 0
1 0
2 1
1 1
2 3
outputCopy
NO
Note
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.
存在n个点,判断连两条直线是否可以使得这些点都在这两条直线上,可以这样考虑,假定存在解,则1可设其在直线A上,而2有两种情况,要么在A上,要么在B上,3也有两种情况,要么在A上,要么在B上,如果这三个点在同一条线上的话,必定是可以确定这条直线的,因为如果说不把这条直线确定下来的话,这三个点中必定存在1个点不能被经过,由此可想到,只要找到两点确定一条直线,如果说这条直线经过了另外的一个点则必定确定该直线。因为只存在两条直线,如果说A直线经过1点的话,2,3点要么存在于A,要么存在于B,1,2,3点等价起来的话,含义在于,1,2,3点至少存在两个点在同一条直线上,枚举一下这3种情况即可,即1,2在同一直线,1,3在同一直线,2,3在同一直线。
#include<iostream>
#include<cstdio>
#include<string>
#include <cstring>
#include <stack>
#include <algorithm>
#include <map>g
#include <queue>
#include <set>
#include <cmath>
using namespace std;
#define INF 0x3f3f3f3f
#define mod 998244353
typedef struct point{
long long x, y;
void getvalue(point& a,point& b) {
x = a.x - b.x;
y = a.y - b.y;
}
}Vector;
point s[100005];
bool flag[100005];
int n;
bool cross(const Vector& a, const Vector& b) {
return (a.x * b.y - a.y * b.x) == 0;
}
bool cmp(const point& a, const point& b) {
return a.x * b.y < a.y * b.x;
}
void test() {
for (int i = 0; i < n; ++i) {
printf("%d:\n", i);
for (int j = i + 1; j < n; ++j) {
for (int k = j + 1; k < n; ++k) {
Vector a, b;
a.getvalue(s[i], s[j]);
b.getvalue(s[i], s[k]);
if (cross(a, b)) {
printf("%d %d\t", j, k);
}
}
}
}
}
bool judge(int x,int y) {
Vector a, b;
a.getvalue(s[x], s[y]);
memset(flag, false, sizeof(flag));
flag[x] = flag[y] = true;
for (int i = 0; i < n; ++i) {
b.getvalue(s[x], s[i]);
if (cross(a, b)) {
flag[i] = true;
}
}
int u[2], k = 0;
for (int i = 0; i < n; ++i) {
if (!flag[i]) {
u[k++] = i;
if (k == 2) {
break;
}
}
}
if (k != 2) {
return true;
}
flag[u[0]] = flag[u[1]] = true;
a.getvalue(s[u[0]], s[u[1]]);
for (int i = 0; i < n; ++i) {
b.getvalue(s[u[0]], s[i]);
if (!cross(a, b) && !flag[i]) {
return false;
}
}
return true;
}
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
scanf("%I64d%I64d", &s[i].x, &s[i].y);
}
if (n < 5) {
cout << "YES" << endl;
}
else {
//1与2在同一条直线上
if (judge(1, 2) || judge(1, 3) || judge(2, 3)) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
return 0;
}