zoukankan      html  css  js  c++  java
  • CF961D Pair Of Lines

    题目描述

    You are given n n 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 n n (1<=n<=105) (1<=n<=10^{5}) (1<=n<=105) — the number of points you are given.

    Then n n n lines follow, each line containing two integers xi x_{i} xi and yi y_{i} yi (∣xi∣,∣yi∣<=109) (|x_{i}|,|y_{i}|<=10^{9}) (xi,yi<=109) — coordinates of i i i -th point. All n n 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.

    输入输出样例

    输入样例#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 1 1 , 3 3 3 and 5 5 5 , and another one containing two remaining points.

    Solution:

      因为用两条直线就能覆盖所有点,所以任取三点,其中一定至少有两点在同一直线上,然后我们枚举其中两点的搭配,再标记所有在这条直线上的点,最后判断未被标记的点是否在同一直线上,只要有一种情况满足就可以,否则无解。

    代码:

    #include<bits/stdc++.h>
    #define il inline
    #define ll long long
    #define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
    #define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
    using namespace std;
    const int N=100005;
    int n;
    bool f[N];
    struct node{
        ll x,y;
    }t[N];
    
    il ll gi(){
        ll a=0;char x=getchar();bool f=0;
        while((x<'0'||x>'9')&&x!='-')x=getchar();
        if(x=='-')x=getchar(),f=1;
        while(x>='0'&&x<='9')a=(a<<3)+(a<<1)+x-48,x=getchar();
        return f?-a:a;
    }
    
    il bool check(int p,int q){
        For(i,1,n) f[i]=((t[p].x-t[i].x)*(t[q].y-t[i].y)==(t[p].y-t[i].y)*(t[q].x-t[i].x)?1:0);
        int l=0,r=0;
        For(i,1,n) 
            if(!f[i]){
                if(!l) l=i;
                else if(!r) r=i;
                else {
                    if((t[l].x-t[i].x)*(t[r].y-t[i].y)!=(t[l].y-t[i].y)*(t[r].x-t[i].x))return 0;
                }
            }
        return 1;
    }
    
    int main(){
        n=gi();
        For(i,1,n) t[i].x=gi(),t[i].y=gi();
        if(n<=3) puts("YES");
        else puts(check(1,2)||check(1,3)||check(2,3)?"YES":"NO");
        return 0;
    }
  • 相关阅读:
    SubtextHTTP 模块
    asp.net控件设计时支持(2)自动格式设置
    新立得字体模糊解决经验
    详解新网银木马清除技巧
    Ubuntu 9.10 29日发布,更新列表已经公布
    小样儿想封我?WebOS 1.2.1再次突破iTunes同步限制
    检测到您尚未安装ALEXA工具条
    在Puppy Linux中安装Firefox
    一步一步教你用ES封装系统(最完整视频教程)
    C# 判断是否为数字
  • 原文地址:https://www.cnblogs.com/five20/p/9375922.html
Copyright © 2011-2022 走看看