CodeForces 849B Tell Your World 点线

来源:互联网 发布:淘宝联盟链接转换 编辑:程序博客网 时间:2024/05/29 15:34

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, thei-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 onexactly one of them, and each of them passes throughat 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 integersy1, 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
57 5 8 6 9
Output
Yes
Input
5-1 -2 0 0 -5
Output
No
Input
55 4 3 2 1
Output
No
Input
51000000000 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 points2, 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.

题意 : 依次给出 y1 y2 y3 ... yn,表示点得纵坐标,每个点的横坐标就是对应的下标

(1,y1) (2,y2)...(n,yn) 问你这些点能不能被两条平行的直线贯穿,即所有点都在这两条直线上,且这两条直线不能平行

思路:对于大问题,先缩小为小问题

首先,只有两个点的时候,随意都可以把它画成两条平行的线,然后是三个点,那么其中一条线要穿过两个点,然后第三个点所在的直线也必须与前一条直线平行,那么我们就可以找出三种方案,就是两两点组合确定好一条直线。

在画了三个点的情况之后,增加一个点,但是我们只能判断这个点是否在前面的两条直线上,也就是说,前面的三个点就已经确定了线的斜率。

也就是说,利用前面三个点得到的三个斜率,必定有一个是正确的,那么我们就只需要枚举那三个斜率,然后判断是否所有点分布在两条为该斜率且不平行的线上就可以了

我枚举斜率之后,对于每个点,计算出过改点且为该斜率情况下的直线经过Y轴的点的纵坐标是多少,然后记录一下又多少个不同的过Y轴纵坐标,只有当数目为2的时候才正确。

#include<iostream>#include<cstdio>#include<algorithm>#include<map>using namespace std;map<double,int>m;int op[1005];double ang[5];int main(){int n,y,flag;double a1,a2,a3;while(scanf("%d",&n) != EOF){for(int i = 1;i <= n;i++){scanf("%d",&op[i]);}ang[1] = (op[2] - op[1]) * 1.0 / (2 - 1);ang[2] = (op[3] - op[2]) * 1.0 / (3 - 2);ang[3] = (op[3] - op[1]) * 1.0 / (3 - 1);flag = 0;for(int a = 1;a <= 3;a++){int cnt = 0;m.clear();for(int i = 1;i <= n;i++){double b = op[i] - ang[a] * i;if(!m.count(b)){m[b] = 1;cnt++;}else{m[b]++;}}if(cnt == 2){flag = 1;break;}}if(flag)puts("Yes");elseputs("No");}return 0;}



原创粉丝点击