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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值