两条平行线覆盖所有点

  1. B. Tell Your World
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Connect the countless points with lines, till we reach the faraway yonder.

    There are n points on a coordinate plane, the i-th of which being (i, yi).

    Determine whether it's possible to draw two parallel and non-overlapping lines, such that every point in the set lies on exactly one of them, and each of them passes through at least one point in the set.

    Input

    The first line of input contains a positive integer n (3 ≤ n ≤ 1 000) — the number of points.

    The second line contains n space-separated integers y1, y2, ..., yn ( - 109 ≤ yi ≤ 109) — the vertical coordinates of each point.

    Output

    Output "Yes" (without quotes) if it's possible to fulfill the requirements, and "No" otherwise.

    You can print each letter in any case (upper or lower).

    Examples
    Input
    5
    7 5 8 6 9
    
    Output
    Yes
    
    Input
    5
    -1 -2 0 0 -5
    
    Output
    No
    
    Input
    5
    5 4 3 2 1
    
    Output
    No
    
    Input
    5
    1000000000 0 0 0 0
    
    Output
    Yes
    
    Note

    In the first example, there are five points: (1, 7)(2, 5)(3, 8)(4, 6) and (5, 9). It's possible to draw a line that passes through points1, 3, 5, and another one that passes through points 2, 4 and is parallel to the first one.

    In the second example, while it's possible to draw two lines that cover all points, they cannot be made parallel.

    In the third example, it's impossible to satisfy both requirements at the same time.

    题意 : 依次给出 y1 y2 y3 ... yn,表示点得纵坐标,每个点的横坐标就是对应的下标

    (1,y1) (2,y2)...(n,yn) 问你这些点能不能被两条平行的直线贯穿,即所有点都在这两条直线上,且这两条直线不能平行

    思路:对于大问题,先缩小为小问题

    首先,只有两个点的时候,随意都可以把它画成两条平行的线,然后是三个点,那么其中一条线要穿过两个点,然后第三个点所在的直线也必须与前一条直线平行,那么我们就可以找出三种方案,就是两两点组合确定好一条直线。

    在画了三个点的情况之后,增加一个点,但是我们只能判断这个点是否在前面的两条直线上,也就是说,前面的三个点就已经确定了线的斜率。

    也就是说,利用前面三个点得到的三个斜率,必定有一个是正确的,那么我们就只需要枚举那三个斜率,然后判断是否所有点分布在两条为该斜率且不平行的线上就可以了

    我枚举斜率之后,对于每个点,计算出过改点且为该斜率情况下的直线经过Y轴的点的纵坐标是多少,然后记录一下又多少个不同的过Y轴纵坐标,只有当数目为2的时候才正确。


  2. #include<iostream>  
  3. #include<cstdio>  
  4. #include<algorithm>  
  5. #include<map>  
  6. using namespace std;  
  7. map<double,int>m;  
  8. int op[1005];  
  9. double ang[5];  
  10. int main(){  
  11.     int n,y,flag;  
  12.     double a1,a2,a3;  
  13.     while(scanf("%d",&n) != EOF){  
  14.         for(int i = 1;i <= n;i++){  
  15.             scanf("%d",&op[i]);  
  16.         }  
  17.         ang[1] = (op[2] - op[1]) * 1.0 / (2 - 1);  
  18.         ang[2] = (op[3] - op[2]) * 1.0 / (3 - 2);  
  19.         ang[3] = (op[3] - op[1]) * 1.0 / (3 - 1);  
  20.         flag = 0;  
  21.         for(int a = 1;a <= 3;a++){  
  22.             int cnt = 0;  
  23.             m.clear();  
  24.             for(int i = 1;i <= n;i++){  
  25.                 double b = op[i] - ang[a] * i;  
  26.                 if(!m.count(b)){  
  27.                     m[b] = 1;  
  28.                     cnt++;  
  29.                 }else{  
  30.                     m[b]++;  
  31.                 }     
  32.             }  
  33.             if(cnt == 2){  
  34.                 flag = 1;  
  35.                 break;  
  36.             }  
  37.         }  
  38.         if(flag)  
  39.             puts("Yes");  
  40.         else  
  41.             puts("No");  
  42.     }  
  43.     return 0;  
  44. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值