zoukankan      html  css  js  c++  java
  • Codeforces Round #431 (Div. 2) B. Tell Your World

    题面:

    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 points 1, 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.

    思路:

      因为一条直线最少过一个点,另一条直线则至少过n-1个点。

      所以直接枚举那过两个点的直线即可,这样是n*n的做法。

      但是其实没必要枚举那么多直线,只需考虑前三条直线即可。

      对于三个点1,2,3

      判断12在同一直线

      13在同一直线
      23在同一直线

      以上有一个符合情况就可以。

      但是要特判掉所有点都在同一直线的情况

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 #define MP make_pair
     6 #define PB push_back
     7 typedef long long LL;
     8 typedef pair<int,int> PII;
     9 const double eps=1e-8;
    10 const double pi=acos(-1.0);
    11 const int K=1e6+7;
    12 const int mod=1e9+7;
    13 
    14 int n,vis[K];
    15 struct Point
    16 {
    17     LL x,y;
    18 }pt[K],pp[K];
    19 LL dc(Point &ta,Point &tb,Point &tc)
    20 {
    21     return (tb.x-ta.x)*(tc.x-ta.x)+(tb.y-ta.y)*(tc.y-ta.y);
    22 }
    23 LL cc(Point &ta,Point &tb,Point &tc)
    24 {
    25     return (tb.x-ta.x)*(tc.y-ta.y)-(tc.x-ta.x)*(tb.y-ta.y);
    26 }
    27 bool check(void)
    28 {
    29     int cnt=0;
    30     for(int i=3;i<=n;i++)
    31     if(cc(pt[1],pt[2],pt[i])!=0)
    32         pp[++cnt]=pt[i];
    33     for(int i=3;i<=cnt;i++)
    34     if(cc(pp[1],pp[2],pp[i])!=0)
    35         return 0;
    36     Point ta,tb,tc;
    37     ta.x=pt[2].x-pt[1].x,ta.y=pt[2].y-pt[1].y;
    38     tb.x=pp[2].x-pp[1].x,tb.y=pp[2].y-pp[1].y;
    39     tc.x=tc.y=0;
    40     return cnt<2||cc(tc,ta,tb)==0;
    41 }
    42 int main(void)
    43 {
    44     int ff=0;cin>>n;
    45     for(int i=1;i<=n;i++)
    46         scanf("%lld",&pt[i].y),pt[i].x=i;
    47     for(int i=3;i<=n&&!ff;i++)
    48     if(cc(pt[i-2],pt[i-1],pt[i])!=0)
    49         ff=1;
    50     if(!ff) {printf("NO
    ");return 0;}
    51     if(check())
    52         {printf("YES
    ");return 0;}
    53     swap(pt[1],pt[3]);
    54     if(check())
    55         {printf("YES
    ");return 0;}
    56     swap(pt[2],pt[3]);
    57     if(check())
    58         {printf("YES
    ");return 0;}
    59     printf("NO
    ");
    60     return 0;
    61 }
  • 相关阅读:
    IsIconic() OnPaint里的用途
    中值滤波
    一个小学生题库生成器
    音视频同步
    [转]字符编码笔记:ASCII,Unicode和UTF8
    项目中常见bug及解决方法
    TSQL基础chp10可编程对象学习笔记[上]
    使用UdpAppender时出现了“使用了与请求协议不兼容的地址”的解决办法
    .net gridview 任意单击某行跳转到新的页面,并且新页面的参数来自于与gridview中的不可见字段
    数组去重的四种方法
  • 原文地址:https://www.cnblogs.com/weeping/p/7465477.html
Copyright © 2011-2022 走看看