849B Tell Your World
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.
这道题当时想的时候怎么也想不出来……..放到B题这么位置,以后千万不敢多想,直接上暴力,这道题知道前3个点以后,肯定能求出斜率, 因为肯定有两个点会构成一条直线符合条件,然后我们枚举前三个点构成的斜率,然后再从第二个点开始依次与第一个点比较,如果跟第一个点的斜率不符合原来假设的斜率就说明这个点要么是第二条直线的起点,要么就构不成直线,这三个斜率只要有一个符合条件都可以
#include<bits/stdc++.h>
using namespace std;
int ans[1005],n;
bool check(double k) {
int point=-1,flag=0;
for(int i=2;i<=n;i++) {
if((ans[i]-ans[1])==k*(i-1)) continue;
flag=1;
if(point==-1) point=i;
if((ans[i]-ans[point])!=k*(i-point)) {
flag=0;
break;
}
}
if(flag==1) return true;
else return false;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++) cin>>ans[i];
double k1=1.0*(ans[2]-ans[1]);
double k2=0.5*(ans[3]-ans[1]);
double k3=1.0*(ans[3]-ans[2]);
if(check(k1)||check(k2)||check(k3)) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}