CodeForces 849B Tell Your World 点线

本文探讨了一种点集覆盖问题,即如何通过两条平行且不重叠的直线覆盖平面上的点集。文章提出了一种解决方案,通过计算不同点组合形成直线的斜率来判断覆盖的可能性,并提供了一个具体的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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, 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.

题意 : 依次给出 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");
		else
			puts("No");
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值