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
5
0 0
0 1
1 1
1 -1
2 2
YES
5
0 0
1 0
2 1
1 1
2 3
NO
题意:给定N个点,问是否可以用一条或者两条直线覆盖所有点。
思路:如果所有点已经在一条直线上,成立。 否则找不共线的三个点,那么其中一条线必定的其中一条,然后验证即可。
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) #define ll long long using namespace std; const int maxn=100010; int N,A,B,C,x[maxn],y[maxn],vis[maxn],num; bool line(int a,int b,int c){ if(x[a]==x[b]&&x[a]==x[c]) return true; if(y[a]==y[b]&&y[a]==y[c]) return true; if((ll)(1LL*x[a]-x[b])*(y[b]-y[c])-(1LL*y[a]-y[b])*(x[b]-x[c])==0) return true; return false; } bool find() { rep(i,3,N) if(!line(1,2,i)) { C=i; return true; } return false; } bool check(int a,int b) { int A=-1,B=-1; rep(i,1,N) vis[i]=0;num=0; rep(i,1,N) if(line(a,b,i)) vis[i]=1,num++; else { if(A==-1) A=i; else if(B==-1) B=i; } if(num==N||num==N-1||num==N-2) return true; rep(i,1,N) if(!vis[i]) if(!line(A,B,i)) return false; return true; } int main() { scanf("%d",&N); rep(i,1,N) scanf("%d%d",&x[i],&y[i]); if(!find()) return puts("YES"),0; if(check(1,2)) return puts("YES"),0; if(check(1,C)) return puts("YES"),0; if(check(2,C)) return puts("YES"),0; puts("NO"); return 0; }