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?
InputThe 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.
OutputIf 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.
Examples5 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.
题意:输出一个n,下面n行每行一个点,判断是否存在两条直线 能使穿过所有的点;
思路:由于现在不会几何模板,所以就用的数学上的知识的写的;
先假设前三个点不在一条直线上,三个不共线的点确定三条直线,那么要是所有的点想在两条直线上,那么一定是在这三条直线的两条上,但是具体写的时候不是从三条直线中任取两条判断,而是从三条直线上任取一条,把在条直线上的点全部标记,在看看没有被标记的点是不是在一条直线上, 这样判断 就算前三个点在一条直线上也没事,等于这一条直线被判断了三次;具体的看代码;
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
#define Max 100010
struct node
{
ll x,y;
}stu[Max];
ll n;
int book[Max];
int check(int ss,int ee)
{
int i;
memset(book,0,sizeof(book));
for(i = 0;i<n;i++)
{
if((stu[ee].x-stu[ss].x)*(stu[i].y-stu[ss].y)==(stu[ee].y-stu[ss].y)*(stu[i].x-stu[ss].x))
book[i] = 1;
}
int a[3];
int sum = 0;
for(i = 0;i<n;i++)
{
if(!book[i])
a[sum++] = i;
if(sum==2)
break;
}
if(sum<2) return 1;
for(i = 0;i<n;i++)
{
if((stu[a[1]].x-stu[a[0]].x)*(stu[i].y-stu[a[0]].y)==(stu[a[1]].y-stu[a[0]].y)*(stu[i].x-stu[a[0]].x))
book[i] = 1;
}
for(i = 0;i<n;i++)
{
if(!book[i])
return 0;
}
return 1;
}
int main()
{
ll i,j;
while(~scanf("%d",&n))
{
for(i = 0;i<n;i++)
scanf("%lld%lld",&stu[i].x,&stu[i].y);
if(n<=4)
{
printf("YES\n");
continue;
}
if(check(0,1)||check(0,2)||check(1,2))
{
printf("YES\n");
}
else printf("NO\n");
}
return 0;
}