Educational Codeforces Round 41 (Rated for Div. 2)D. Pair Of Lines

本文探讨了给定平面上的N个整数坐标点,是否可以通过绘制两条直线使得每个点至少位于一条直线上。通过检查前三个点是否至少存在两个共线的点,并将共线的点进行标记的方法,来解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Pair Of Lines
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
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
Copy
5
0 0
0 1
1 1
1 -1
2 2
output
Copy
YES
input
Copy
5
0 0
1 0
2 1
1 1
2 3
output
Copy
NO
Note

In the first example it is possible to draw two lines, the one containing the points 13 and 5, and another one containing two remaining points.

思路:

假设成立,那么前三个点一定不会全都不在一条直线上,一定有两个点共线。那么就可以从前三个点枚举共线的两个点。将与其共线的点全部标记。再寻找两个没被标记的点,如果找不够两个点,那么这一定是一组解,将与这两个点共线的点标记。如果所有点都被标记,那么这就是一组解。


代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<math.h>
using namespace std;
#define MAXN 100005
#define LL long long
int n;
struct node
{
    LL x,y;
}p[MAXN];
bool vis[MAXN];
int check(int a,int b)
{
    memset(vis,0,sizeof vis);
    for(int i=0;i<n;i++)
    {
        if((p[a].x-p[b].x)*(p[i].y-p[a].y)==(p[a].y-p[b].y)*(p[i].x-p[a].x))
            vis[i]=1;
    }
    int c[2],cnt=0;
    for(int i=0;i<n;i++)
    {
        if(!vis[i])
        {
            c[cnt++]=i;
        }
        if(cnt==2) break;
    }
    if(cnt<2) return 1;
    for(int i=0;i<n;i++)
    {
        if((p[c[0]].x-p[c[1]].x)*(p[i].y-p[c[0]].y)==(p[c[0]].y-p[c[1]].y)*(p[i].x-p[c[0]].x))
            vis[i]=1;
    }
    for(int i=0;i<n;i++)
        if(!vis[i]) return 0;
    return 1;
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%lld%lld",&p[i].x,&p[i].y);
    }
    if(n<=4) puts("YES");
    else
    {
        if(check(0,1) || check(0,2) || check(1,2))
            puts("YES");
        else puts("NO");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值