Educational Codeforces Round 41 D. Pair Of Lines

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;
}

  

posted @ 2018-04-10 08:39  HTWX  阅读(136)  评论(0编辑  收藏  举报