B. Tell Your World Codeforces Round #431 (Div. 2)(简单模拟)
来源:互联网 发布:淘宝怎么看退换货记录 编辑:程序博客网 时间:2024/05/18 00:49
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 ( - 10^9 ≤ yi ≤ 10^9) — 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.
大致题意:在二维平面上有n个点,告诉你这n个点的坐标,然后问你是否能找出两条不重合的平行线,使得所有的点,一部分在其中一条线上,另外一部分在另一条线上。
思路:注意到所有的点x坐标都不同,所以过两点的直线的斜率一定存在。假设存在满足条件的两条平行线a,b,那么所有的点不是在直线a上就是在直线b上,所以我们可以先求出1号点与其他所有点的斜率,然后从中选择一个斜率k作为当前一条平行线的斜率。接下来遍历所有的点,如果某个点与1号点的斜率不为k,说明这个点应该在另一条平行线上,记录一下,然后判断这些点是否能组成另一条平行线且斜率为k。具体实现看代码。
注意:如果所有的点都在同一条直线上是不行的。
代码如下
#include <iostream> #include <cstdio>#include <cmath>#include <algorithm>#include <cstring>#include <queue>#include <cstdio>#include <map>using namespace std; int a[1005];//记录每个点的纵坐标,横坐标即编号double ans[1005];//记录编号2~n的点与编号1连线的斜率int main(){ int n; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); if(i!=1) { ans[i]=(a[i]-a[1])*1.0/(i-1); } } int flag1=0; for(int i=2;i<=n;i++) { double k1=ans[i];//选择一个斜率 int f=0;//记录斜率不同的点的个数 int x; int flag2=1; for(int j=2;j<=n;j++)//遍历其它点 { if(j==i) continue; if(ans[j]!=k1)//如果点j与点1的连线斜率不为k1,说明应该在另一条平行线上 { if(f==0)//如果是第一个斜率不同的点 { x=j; f++; } else//否则此时的点j与第一个斜率不同的点的连线的斜率应该为k1 { f++; if((a[j]-a[x])*1.0/(j-x)!=k1)//如果不是k1,说明此时选择的点i和点1构成的平行线是错的,结束循环,寻找其它可能情况 { flag2=0; break; } } } } if(f==0)//如果所有的点都在用一条直线上 { printf("No\n"); return 0; } if(flag2)//如果此时的情况满足条件,结束循环 { flag1=1; break; } } if(flag1) printf("Yes\n"); else //此时还有一种情况就是点1单独,其余的点都在都一条直线上 { double k=(a[3]-a[2])*1.0; for(int i=4;i<=n;i++) { if((a[i]-a[2])*1.0/(i-2)!=k) { printf("No\n"); return 0; } } printf("Yes\n"); }}
- B. Tell Your World Codeforces Round #431 (Div. 2)(简单模拟)
- Codeforces Round #431 (Div. 2) B. Tell Your World(技巧性模拟+精度坑死人系列)
- Codeforces Round #431 (Div. 2) B. Tell Your World
- Codeforces Round #431 (Div. 2)B Tell Your World
- Codeforces Round #431 (Div. 2) Tell Your World
- Codeforces Round #431 (Div. 2) B. Tell Your World(向量积)
- Codeforces Round #431 (Div. 2) 849B Tell Your World(暴力+枚举)
- Codeforces Round #431 (Div. 2) A、Odds and Ends B、Tell Your World C、From Y to Y
- CodeForces #431Div. 2 849B Tell your world 几何 暴力 枚举
- Codeforces 849 B Tell Your World
- CodeForces 849B Tell Your World 点线
- codeforces 849B. Tell Your World
- codeforces 849B Tell Your World
- Codeforces-849B Tell Your World
- Codeforces 894B. Tell Your World
- Codeforces Round #107 (Div. 2) B (简单模拟)
- codeforces 431B Tell Your World(两平行线穿过所有点)【模板】
- Codeforces Tell Your World
- php 过狗一句话木马的编写
- 数据挖掘python学习——《写给程序员的数据挖掘实践指南》第2章
- R语言:leafletCN的应用
- 准备第一次投稿 SCI 论文,有哪些要注意的地方?
- Fast Power--lintcode
- B. Tell Your World Codeforces Round #431 (Div. 2)(简单模拟)
- Textview部分内容设置颜色、字体、超链接、图片
- 使用Git上传项目代码到Github
- maven实践:(一)创建普通java项目、打包、运行
- 如何提升绩效
- 二叉树中和为某一值的路径
- Hdu 6081 度度熊的王国战略 无向图全局最小割
- 静态链接器构造
- 消息中间件核心实体(0)