Educational Codeforces Round 41 D. Pair Of Lines

探讨如何用两条直线覆盖平面上的多个整数坐标点,通过枚举前三个点构成的直线并判断其余点是否共线,实现算法解决。面对复杂判定与枚举,采用直接判断相等而非斜率的方法,确保正确性和效率。

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

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 (1 ≤ n ≤ 105) — the number of points you are given.

Then n lines follow, each line containing two integers xi and y(|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
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.

  题目大意就是平面上一堆点,问你是否可以只用两条直线把这些点全部覆盖。。。

  开始做的时候没有思路。。。然后wmy说这不是SB题吗。。。我:OwO。。。好强啊。。。

  wmy说大概就是枚举前3个点所构成的所有直线。。。

  然后试着写了写。。。各种判定,枚举,代码写了150+,然后各种WA。。。现在感觉可能是判断共线的部分写错了。。。不应该求斜率的,应该直接变形,判断相等。。。

  今天看题解,发现其实差不多,只不过题解上说,假如点1和点2不在同一条直线上,点1和点3也不在同一条直线上,那么点2就和点3在一条直线上,这样做就好了。。。

  不过要开long long。。。

  

//#include<bits/stdc++.h>
#include <iostream>

#define inf 1000000000
#define maxn 200000+5
#define maxm 5000000+5
#define eps 1e-10
#define ll long long
#define for0(i, n) for(int i=0;i<=(n);i++)
#define for1(i, n) for(int i=1;i<=(n);i++)
#define for2(i, x, y) for(int i=(x);i<=(y);i++)
#define for3(i, x, y) for(int i=(x);i>=(y);i--)
#define for4(i, x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
using namespace std;

ll read() {
    ll x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = 10 * x + ch - '0';
        ch = getchar();
    }
    return x * f;
}

struct poi {
    ll x, y;
} a[maxn];
int v[maxn];
int n;

bool judge(int c, int d, int e) {
    return (a[c].y - a[d].y) * (a[c].x - a[e].x) == (a[c].y - a[e].y) * (a[c].x - a[d].x);
}

int main() {
    n = read();
    for (int i = 1; i <= n; i++) {
        a[i].x = read();
        a[i].y = read();
    }
    if (n <= 3)cout << "YES";
    else {
        int sum = 0, ji1, ji2;
        for (int i = 3; i <= n; i++)
            if (!judge(1, 2, i)) {
                ji2 = ji1;
                ji1 = i;
                sum++;
                v[i] = 1;
            }
        if (sum < 3) {
            cout << "YES";
            return 0;
        } else {
            int flag = 0;
            for (int i = ji2 - 1; i >= 3; i--)
                if (v[i] && (!judge(ji1, ji2, i))) {
                    flag = 1;
                    break;
                }
            if (!flag) {
                cout << "YES";
                return 0;
            } else {
                sum = 0;
                ji1 = ji2 = 0;
                memset(v, 0, sizeof(v));
                swap(a[2], a[3]);
                for (int i = 3; i <= n; i++)
                    if (!judge(1, 2, i)) {
                        ji2 = ji1;
                        ji1 = i;
                        sum++;
                        v[i] = 1;
                    }
                if (sum < 3) {
                    cout << "YES";
                    return 0;
                } else {
                    flag = 0;
                    for (int i = ji2 - 1; i >= 3; i--)
                        if (v[i] && (!judge(ji1, ji2, i))) {
                            flag = 1;
                            break;
                        }
                    if (!flag) {
                        cout << "YES";
                        return 0;
                    } else {
                        swap(a[1], a[3]);
                        sum = 0;
                        ji1 = ji2 = 0;
                        memset(v, 0, sizeof(v));
                        for (int i = 3; i <= n; i++)
                            if (!judge(1, 2, i)) {
                                ji2 = ji1;
                                ji1 = i;
                                sum++;
                                v[i] = 1;
                            }
                        if (sum < 3) {
                            cout << "YES";
                            return 0;
                        } else {
                            flag = 0;
                            for (int i = ji2 - 1; i >= 3; i--)
                                if (v[i] && (!judge(ji1, ji2, i))) {
                                    flag = 1;
                                    break;
                                }
                            if (!flag) {
                                cout << "YES";
                                return 0;
                            } else {
                                cout << "NO";
                                return 0;
                            }
                        }
                    }
                }
            }

        }
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/htwx/articles/8776185.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值