Codeforces 961D Pair Of Lines

3 篇文章 0 订阅
2 篇文章 0 订阅
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;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值