Codeforces 961D Pair Of Lines

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
5
0 0
0 1
1 1
1 -1
2 2
Output
Copy
YES
Input
5
0 0
1 0
2 1
1 1
2 3
Output
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个点,然后问你这些点是否都在两条直线的其中一条上。

解题思路:温故了一下初中求斜率的方法,基础不扎实,做起来就是费劲。

我们都知道三点确定一条直线,那么我们就拿这三个点(假设a,b,c)来说,k=(b.y-a.y)/(b.x-a.x)=(c.y-b.y)/(c.x-b.x)。

推导一下,也就是(b.y-a.y)*(c.x-b.x)=(c.y-b.y)*(b.x-a.x);

当我们假设有两个点是在同一条边的时候,我们可以暴力枚举剩下没有走过的点,如果满足这个等式的话,就说明这一个点也是在同一条边上的。然后我们用同样的方式来确定剩下的点是否在同一条边。

一开始我们需要选取三个点,这三个点虽然可以是一条直线,但是也可以是一个三角线。为了对应三角线,那么他的三条边都有可能是一条公共边。(切记切记)。 新技能,GET!!!.

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
struct Node{
	double x,y;
}node[maxn];
int n,m,vis[maxn];
bool judge(Node a,Node b,Node c){
	double k1=(b.y-a.y)*(c.x-b.x);
	double k2=(c.y-b.y)*(b.x-a.x);
	if(k1==k2) return true;
	return false;
}
bool check(int a,int b){
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=n;i++){
		if(judge(node[a],node[b],node[i]))
			vis[i]=1;
	}
	int p1=-1,p2=-1;
	for(int i=1;i<=n;i++){
		if(!vis[i]){
			if(p1==-1) p1=i;
			else p2=i;
		}
	}
	if(p1==-1||p2==-1) return true;
	else{
		for(int i=1;i<=n;i++){
			if(!vis[i]){
				if(!judge(node[p1],node[p2],node[i]))  return false;
			} 
		}
		return true;
	}
}
int main(){
	int i,j;
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		scanf("%lf%lf",&node[i].x,&node[i].y);
	}
	if(check(1,2)||check(1,3)||check(2,3))
		printf("YES\n");
	else
		printf("NO\n");
	return 0;
}







猜你喜欢

转载自blog.csdn.net/bao___zi/article/details/79900671
今日推荐