Codeforce 961-D Pair Of Lines (计算几何)

Problem Description

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.

Sample Input    

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

Sample Output    

YES

Sample Input    

5
0 0
1 0
2 1
1 1
2 3

Sample Output    

NO

题意:

给你n个点,问你能否用两条直线,问你是否能用2条直线过所有点。

思路:

对于此题,当n<=4是一定成立的,n>4的时候我们可以任意取3个点,先以3点中的2点画一条直线,把该直线不能过的点存入vector中,再对剩下的所有点进行判断,它们是否都在一条直线上,是的话输出YES,不是则取3点中其他2点重复进行上步骤,直到3种情况都没办法实现,那么可以输出NO了。

原理:若能用2条直线经过所有点,那么取任意三点中的某一条两点连线必然与其中一条直线重合

 

代码:

#include <cstdio>  
#include <string>  
#include <cstring>  
#include <cmath>  
#include <iostream>  
#include <algorithm>   
#include<vector>  
using namespace std;  
#define inf 0x3f3f3f3f  
#define  ll long long   
const int maxn = 1e5 + 500;  
struct node  
{  
    ll x, y;  
}a[maxn];  
bool X(node p1, node p2, node p0)  
{  
    return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y) == 0 ? true : false;  
}  
int n;  
int vis[maxn];  
bool fun()  
{  
    vector<int>q;  
    int key = 1;  
    for (int i = 2; i < n; i++)  
        if (!X(a[0], a[1], a[i]))q.push_back(i);  
    if (q.size() <= 2)return true;  
    for (int i = 2; i < q.size(); i++)  
        if (!X(a[q[0]], a[q[1]], a[q[i]])) {  
            key = 0; break;  
        }  
    if (key)return true;  
    q.clear(); key = 1;  
    for (int i = 1; i < n; i++)  
        if (!X(a[0], a[2], a[i]))q.push_back(i);  
    if (q.size() <= 2)return true;  
    for (int i = 2; i < q.size(); i++)  
        if (!X(a[q[0]], a[q[1]], a[q[i]])) {  
            key = 0; break;  
        }  
    if (key)return true;  
    q.clear(); key = 1;  
    for (int i = 0; i < n; i++)  
        if (!X(a[1], a[2], a[i]))q.push_back(i);  
    if (q.size() <= 2)return true;  
    for (int i = 2; i < q.size(); i++)  
        if (!X(a[q[0]], a[q[1]], a[q[i]])) {  
            key = 0; break;  
        }  
    if (key)return true;  
    return false;  
}  
int main()  
{  
    cin >> n;  
    for (int i = 0; i < n; i++)  
        scanf("%lld%lld", &a[i].x, &a[i].y);  
    if (n<=4||fun())  
        cout << "YES" << endl;  
    else  
        cout << "NO" << endl;  
    return 0;  
}  


 

猜你喜欢

转载自blog.csdn.net/wcxyky/article/details/89846972
今日推荐