codeforces 849B
2017-09-02 10:59
183 查看
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, 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
output
input
output
input
output
input
output
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个点(i,y[i])问你能否构成两条不重合的平行直线,每条直线最少包含一个点。所有点都在这两条直线上。
思路:两直线平行判断斜率是否相等就可以了。先取1,2,3个点默认为最下方连续三个点。计算出这三个点两两之间的斜率,记为k1,k2,k3。显然两条直线斜率一定在这三个中。然后以1点为基准点检验是否满足即可。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e3+5;
int y[maxn],n;
bool solve(double k)
{
bool flag=false;
int point=-1;
for(int i=2;i<=n;i++)
{
if(y[i]-y[1]==k*(i-1)) continue;//一条
flag=true;//有两条
if(point<0) point=i;//实际上是在记录条数
else if(y[i]-y[point]!=k*(i-point))//不相等说明不止两条
{
flag=false;
break;
}
}
if(flag) return true;
return false;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&y[i]);
}
double k1=1.0*(y[2]-y[1]);//精度问题还是注意下
double k2=0.5*(y[3]-y[1]);
double k3=1.0*(y[3]-y[2]);
if(solve(k1)||solve(k2)||solve(k3)) puts("Yes");
else puts("No");
}
return 0;
}
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, 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.
题意:给你n个点(i,y[i])问你能否构成两条不重合的平行直线,每条直线最少包含一个点。所有点都在这两条直线上。
思路:两直线平行判断斜率是否相等就可以了。先取1,2,3个点默认为最下方连续三个点。计算出这三个点两两之间的斜率,记为k1,k2,k3。显然两条直线斜率一定在这三个中。然后以1点为基准点检验是否满足即可。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e3+5;
int y[maxn],n;
bool solve(double k)
{
bool flag=false;
int point=-1;
for(int i=2;i<=n;i++)
{
if(y[i]-y[1]==k*(i-1)) continue;//一条
flag=true;//有两条
if(point<0) point=i;//实际上是在记录条数
else if(y[i]-y[point]!=k*(i-point))//不相等说明不止两条
{
flag=false;
break;
}
}
if(flag) return true;
return false;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&y[i]);
}
double k1=1.0*(y[2]-y[1]);//精度问题还是注意下
double k2=0.5*(y[3]-y[1]);
double k3=1.0*(y[3]-y[2]);
if(solve(k1)||solve(k2)||solve(k3)) puts("Yes");
else puts("No");
}
return 0;
}
相关文章推荐
- Blown Garland_Codeforces
- Codeforces 679C Bear and Square Grid 暴力(滑动窗口)
- codeforces 286(Round #176)题解
- CodeForces 535B — Tavas and SaDDas
- codeforces 706D. Vasiliy's Multiset 带删除操作的字典树(真模版)
- [codeforces 886F]Symmetric Projections
- CodeForces 584A
- CodeForces - 663A
- Codeforces 28B. pSort 连通性
- // codeforces 471D // kmp初学
- Codeforces 526F 分治
- CodeForces 575 H.Bots(组合数学)
- CodeForces - 724A
- codeforces 523D tatistics of Recompressing Videos
- Codeforces 103B. Cthulhu 寻找奈亚子
- codeforces 540 B School Marks【贪心】
- CodeForces 25C(Floyd)
- codeforces 449D - Jzzhu and Numbers 高维前缀和 容斥
- Codeforces 769D k-Интересные пары чисел
- CodeForces 840E. In a Trap