Codeforces Round #431 (Div. 2) B

Posted 樱花落舞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

 

以上是关于Codeforces Round #431 (Div. 2) B的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #431 (Div. 2) A

Codeforces Round #431 (Div. 2) B

Codeforces Round #431 Div. 1

codeforces比赛题解#849 CF Round #431 (Div.2)

Codeforces Round #431 (Div. 1) D.Shake It!

Codeforces Round #436 E. Fire(背包dp+输出路径)