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

    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.

    解法:把点分成两条平行线的形式,能不能分成呢

    解法:

    1 暴力啦

    2 我们把1和i的斜率算一算,然后符合条件的都标记

    3 剩下的再算一算斜率,然后发现有没有不等于这个斜率的,不等于返回第二步 i+1

    4 因为我们1这个点已经当做确定点了,我们需要特殊考虑一下 比如 1 4 5 6这种情况

    5 还有其他细节自己判断一下

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 double x[1234];
     4 set<double>Se;
     5 double ans;
     6 int main(){
     7     int n;
     8     int flag=0;
     9     cin>>n;
    10     cin>>x[1];
    11     cin>>x[2];
    12     ans=x[2]-x[1];
    13     for(int i=3;i<=n;i++){
    14         cin>>x[i];
    15         if(x[i]-x[i-1]!=ans){
    16             flag=1;
    17         }
    18     }
    19     if(x[3]-x[2]!=ans){
    20         int flag3=0;
    21         double cnt=x[3]-x[2];
    22         for(int i=4;i<=n;i++){
    23             if(x[i]-x[i-1]!=cnt){
    24                 flag3=1;
    25             }
    26         }
    27         if(flag3==0){
    28             cout<<"Yes"<<endl;
    29             return 0;
    30         }
    31     }
    32     for(int i=1;i<=n;i++){
    33         Se.insert(x[i]);
    34     }
    35     if(Se.size()==1){
    36         cout<<"No"<<endl;
    37         return 0;
    38     }
    39     if(Se.size()==2){
    40         cout<<"Yes"<<endl;
    41         return 0;
    42     }
    43     if(flag==0){
    44         cout<<"No"<<endl;
    45         return 0;
    46     }else{
    47         map<int,int>Mp;
    48         double lv;
    49         Mp[1]=1;
    50         for(int i=2;i<=n;i++){
    51             Mp.clear();
    52             Mp[i]=1;
    53             lv=(x[i]-x[1])/(i-1)*1.0;
    54           //  cout<<lv<<" "<<i<<endl;
    55             for(int j=2;j<=n;j++){
    56                 if(Mp[j]) continue;
    57                 //cout<<(x[j]-x[1])/(j-1)<<" "<<j<<endl;
    58                 if((x[j]-x[1])/(j-1)==lv){
    59                     Mp[j]=1;
    60                 }
    61             }
    62             int x1;
    63             for(int j=2;j<=n;j++){
    64                 if(Mp[j]==0){
    65                     x1=j;
    66                     Mp[j]=1;
    67                     break;
    68                 }
    69             }
    70           //  cout<<x1<<endl;
    71             int flag1=0;
    72             for(int j=2;j<=n;j++){
    73                 if(Mp[j]==0){
    74                 //    cout<<(x[j]-x[x1])/(j-x1)<<"B"<<x1<<" "<<j<<endl;
    75                     if((x[j]-x[x1])/(j-x1)!=lv){
    76                         flag1=1;
    77                     }
    78                 }
    79             }
    80             if(flag1==0){
    81                 cout<<"Yes"<<endl;
    82                 return 0;
    83             }
    84         }
    85     }
    86     cout<<"No"<<endl;
    87     return 0;
    88 }

     官方题解:我们只要讨论1 2 3点就行。不管怎么分,平行线都会经过这3点,复杂度变为O(n)

     1 #include <bits/stdc++.h>
     2 #define eps 1e-7
     3 using namespace std;
     4 int read()
     5 {
     6     int x=0,f=1;char ch=getchar();
     7     while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
     8     while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
     9     return x*f;
    10 }
    11 int n,a[1007];
    12 bool vis[1007];
    13 bool check(double k,int b)
    14 {
    15     memset(vis,false,sizeof(vis));
    16     int cnt=0;
    17     for (int i=1;i<=n;i++)
    18     {
    19         if (a[i]-b==1LL*k*(i-1)) 
    20         {
    21             vis[i]=true;
    22             ++cnt;
    23         }
    24     }
    25     if (cnt==n) return false;
    26     if (cnt==n-1) return true;
    27     int pos1=0;
    28     for (int i=1;i<=n;i++)
    29         if (!vis[i]&&pos1==0) pos1=i;
    30     for (int i=pos1+1;i<=n;i++)
    31         if (!vis[i])
    32         {
    33             if (fabs((double)(a[i]-a[pos1])/(i-pos1)-k)>eps) return false;
    34         }
    35     return true;
    36 }
    37 int main()
    38 {
    39     n=read();
    40     for (int i=1;i<=n;i++)
    41         a[i]=read();
    42     bool ans=false;
    43     ans|=check(1.0*(a[2]-a[1]),a[1]);
    44     ans|=check(0.5*(a[3]-a[1]),a[1]);
    45     ans|=check(1.0*(a[3]-a[2]),a[2]*2-a[3]);
    46     if (ans) printf("Yes
    "); else printf("No
    ");
    47     return 0;
    48 }
  • 相关阅读:
    在微信小程序中使用富文本转化插件wxParse
    在微信小程序的JS脚本中使用Promise来优化函数处理
    结合bootstrap fileinput插件和Bootstrap-table表格插件,实现文件上传、预览、提交的导入Excel数据操作流程
    基于Metronic的Bootstrap开发框架经验总结(16)-- 使用插件bootstrap-table实现表格记录的查询、分页、排序等处理
    python 垃圾回收——分代回收 和java有些区别 注意循环引用无法被回收
    python del语句作用在变量上,而不是数据对象(常量)上
    for循环中的lambda与闭包——Python的闭包是 迟绑定 , 这意味着闭包中用到的变量的值,是在内部函数被调用时查询
    Python-try except else finally有return时执行顺序探究——finally语句无论如何也是会执行的
    你所不知道的Python | 字符串连接的秘密——连接大量字符串时 join和f-string都是性能最好的选择
    十大经典排序算法
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/7467322.html
Copyright © 2011-2022 走看看