【CF961D】Pair Of Lines

CF961D 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?

输入格式

The first line contains one integer n (1 <= n <= 1e5) — the number of points you are given.
Then n lines follow, each line containing two integers x[i] and y[i] (|x[i]|, |y[i]| <= 1e9) — coordinates of i-th point. All n points are distinct.

输出格式

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.

题意翻译

题目描述

在平面直角坐标系上给出 n 个点,求是否存在两条直线穿过所有点。

输入格式

第一行一个正整数 n (1 <= n <= 1e5) 表示点数,接下来 n 行每行两个整数 x[i] 和 y[i] (|x[i]|, |y[i]| <= 1e9) 表示一个点。保证不存在两个点的 x[i], y[i] 均相同。

输出格式

如果存在方案输出YES,否则输出NO

输入输出样例

输入 #1

5
0 0
0 1
1 1
1 -1
2 2

输出 #1

YES

输入 #2

5
0 0
1 0
2 1
1 1
2 3

输出 #2

NO

说明/提示

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.
In the first example it is possible to draw two lines, the one containing the points
参考代码: Python
n = int(input())
p = [list(map(int, input().split())) for _ in range(n)]


def f(a, b, c):
    return (b[0] - a[0]) * \
        (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])


def g(fi, se, p):
    q = []
    for x in p:
        if f(fi, se, x):
            if len(q) < 2:
                q.append(x)
            else:
                if f(q[0], q[1], x):
                    return 1
    return 0


print('NO' if n > 4 and all([g(p[0], p[1], p), g(
    p[0], p[2], p), g(p[1], p[2], p)]) else 'YES')
参考代码: C++
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")

#include "bits/stdc++.h"

#define mem(x) memset((x), 0, sizeof((x)))
#define il __attribute__((always_inline))

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

#if __cplusplus > 201403L
#define r
#else
#define r register
#endif

#define c const

namespace _c
{
c double pi = acos(-1.0);
namespace min
{
c int i8 = -128;
c int i16 = -32768;
c int i = -2147483647 - 1;
c ll l = -9223372036854775807LL - 1;
} // namespace min

namespace max
{
c int i8 = 127;
c int i16 = 32767;
c int i = 2147483647;
c ll l = 9223372036854775807LL;
} // namespace max
} // namespace _c

namespace _f
{
template <typename T>
inline c T gcd(T m, T n)
{
    while (n != 0)
    {
        T t = m % n;
        m = n;
        n = t;
    }
    return m;
}

template <typename T>
inline c T abs(c T &a)
{
    return a > 0 ? a : -a;
}

template <typename T>
inline T pow(T a, T b)
{
    T res = 1;
    while (b > 0)
    {
        if (b & 1)
        {
            res = res * a;
        }
        a = a * a;
        b >>= 1;
    }
    return res;
}

template <typename T>
inline T pow(T a, T b, c T &m)
{
    a %= m;
    T res = 1;
    while (b > 0)
    {
        if (b & 1)
        {
            res = res * a % m;
        }
        a = a * a % m;
        b >>= 1;
    }
    return res % m;
}
} // namespace _f

namespace io
{
template <typename T>
inline T read()
{
    r T res = 0, neg = 1;
    char g = getchar();
    for (; !isdigit(g); g = getchar())
    {
        if (g == '-')
        {
            neg = -1;
        }
    }
    for (; isdigit(g); g = getchar())
    {
        res = res * 10 + g - '0';
    }
    return res * neg;
}
template <typename T>
inline void read(T &t)
{
    t = read<T>();
}
template <typename T>
inline void readln(c T first, c T last)
{
    for (r T it = first; it != last; it++)
    {
        read(*it);
    }
}

template <typename T>
inline void _write(T x)
{
    if (x < 0)
    {
        putchar('-');
        x = -x;
    }
    if (x > 9)
    {
        _write(x / 10);
    }
    putchar(x % 10 + '0');
}
template <typename T>
inline void write(c T &x, c char &sep = ' ')
{
    _write(x);
    putchar(sep);
}
template <typename T>
inline void writeln(c T &x)
{
    write(x, '\n');
}
template <typename T>
inline void writeln(c T first, c T last, c char &sep = ' ', c char &ends = '\n')
{
    for (r T it = first; it != last; it++)
    {
        write(*it, sep);
    }
    putchar(ends);
}

#if __cplusplus >= 201103L
template <typename T, typename... Args>
void read(T &x, Args &... args)
{
    read(x);
    read(args...);
}
#endif
} // namespace io
#undef c
#undef r

typedef ll used_type;
typedef pair<used_type, used_type> P;

int n;
vector<P> p;

inline used_type f(P a, P b, P c)
{
    return (b.first - a.first) * (c.second - a.second) - (b.second - a.second) * (c.first - a.first);
}

inline bool g(P fi, P se)
{
    vector<P> q;

    for (const auto &x : p)
    {
        if (f(fi, se, x))
        {
            if (q.size() < 2)
            {
                q.emplace_back(x);
            }
            else
            {
                if (f(q[0], q[1], x))
                {
                    return 1;
                }
            }
        }
    }

    return 0;
}

int main()
{
    io::read(n);
    for (int i = 1; i <= n; i++)
    {
        p.emplace_back(P{io::read<used_type>(), io::read<used_type>()});
    }

    puts((n > 4 && (g(p[0], p[1]) && g(p[0], p[2]) && g(p[1], p[2]))) ? "NO" : "YES");
}

评论

此博客中的热门博文

将博客部署到星际文件系统(IPFS)

高中地理必修一知识点总结

一场CF的台前幕后(下)