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\n"); else printf("No\n");
47     return 0;
48 }
复制代码

 

posted @   樱花落舞  阅读(233)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示