B
来源:互联网 发布:c语言1到100素数和 编辑:程序博客网 时间:2024/04/29 18:25
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.
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 "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).
57 5 8 6 9
Yes
5-1 -2 0 0 -5
No
55 4 3 2 1
No
51000000000 0 0 0 0
Yes
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.
思路:直接枚举前三个点构成的斜率,其中至少有一个是标准的斜率,这样的话分别以这三个斜率作为标准斜率,判断是否构成两条线就可以了。
#include<bits/stdc++.h>using namespace std;int s[3000],n;bool judge(double k){ int p=-1; for(int i=2; i<=n; i++) { if(s[i]-s[1]==(i-1)*k) continue; if(p==-1) p=i; else if(s[i]-s[p]!=(i-p)*k) return 0; } return p!=-1;}int main(){ while(scanf("%d",&n)!=EOF) { for(int i=1; i<=n; i++) scanf("%d",&s[i]); if(judge(s[2]-s[1])||judge((s[3]-s[1])*1.0/2)||judge(s[3]-s[2])) printf("Yes\n"); else printf("No\n"); }}
- b
- b
- b
- //b
- B
- b
- B
- B
- B
- B
- b
- B
- B
- B
- B
- B
- B
- b
- Json对象与Json字符串互转(4种转换方式)
- 关于mysql处理百万级以上的数据时如何提高其查询速度的方法
- LeetCode 342. Power of Four
- POJ
- bzoj2959(LCT+缩点)
- B
- windows update更新失败错误代码80070490
- linux下安装sublime的命令,我的是ubunto 16.04,亲测
- Poj 1384 Piggy-Bank 完全背包
- win 10 双击“我的电脑”没反应
- C++ 虚函数表解析
- 【HTML5学习笔记】35:CSS传统布局 上
- JavaScript事件委托的技术原理
- 《构建之法》目录及摘要