判断平面的一堆点是否在两条直线上

D. Pair Of Lines
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.

Examples
Input
Copy
5
0 0
0 1
1 1
1 -1
2 2
Output
YES
Input
Copy
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.

 

题意 : 给定平面的一些点,问是否最多用两条直线去描绘出这些点。

思路分析 : 对于平面一堆点中的任意3个,若其可以用两条直线去表示,则一定 1 3 或者 2 3 或者 1 2 是在一起的。那么就根据这个去判断每个点就行。

注意斜率 ! 判断两个斜率是否相等的时候需要用乘的关系去判断,除的关系去判断时若斜率不存在时,则会出现问题,分母为 0 了

注意 long long

代码示例 :

#define ll long long
const ll maxn = 1e5+5;
const double pi = acos(-1.0);
const ll inf = 0x3f3f3f3f;

ll n;
struct node
{
    ll x, y;
}pre[maxn];
bool vis[maxn];

bool fun(node a, node b, node c){
    ll k1 = (b.y-a.y)*(c.x-a.x);
    ll k2 = (c.y-a.y)*(b.x-a.x);
    return k1==k2?true:false;
}

bool check(ll a, ll b){
    
    memset(vis, false, sizeof(vis));
    vis[a] = vis[b] = true;
    
    for(ll i = 1; i <= n; i++){
        if (i == a || i == b) continue;
        if (fun(pre[a], pre[b], pre[i])) vis[i] = true;
    }
    ll p1=-1, p2=-1;
    for(ll i = 1; i <= n; i++){
        if (!vis[i]){
            if(p1 == -1) {p1=i;}
            else if (p2==-1){p2=i;}
        }
    }
    if (p1==-1||p2==-1) return true;
    for(ll i = 1; i <= n; i++){
        if (i == p1 || i == p2) continue;
        if (!vis[i] && !fun(pre[p1], pre[p2], pre[i])) return false;
    }
    return true;
}

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    cin >> n;
    for(ll i = 1; i <= n; i++){
        scanf("%lld%lld", &pre[i].x, &pre[i].y);        
    }
    if (n < 5 || check(1, 2) || check(1, 3) || check(2, 3)){
        printf("YES\n");
    }
    else printf("NO\n");
    return 0;
}

 

转载于:https://www.cnblogs.com/ccut-ry/p/8734329.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值