CF849B Tell Your World

Tell Your World

题目

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

解释

我觉得涛哥解释的不错,copy一下

依次给出 y1 y2 y3 … yn,表示点得纵坐标,每个点的横坐标就是对应的下标
(1,y1) (2,y2)…(n,yn) 问你这些点能不能被两条平行的直线贯穿,即所有点都在这两条直线上,且这两条直线不能平行

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

首先,只有两个点的时候,随意都可以把它画成两条平行的线,然后是三个点,那么其中一条线要穿过两个点,然后第三个点所在的直线也必须与前一条直线平行,那么我们就可以找出三种方案,就是两两点组合确定好一条直线。
在画了三个点的情况之后,增加一个点,但是我们只能判断这个点是否在前面的两条直线上,也就是说,前面的三个点就已经确定了线的斜率。

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

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

STL map
map.count()
返回的是被查找元素的个数。注意:map中不存在相同元素,所以返回值只能是1或0。
另外
map可以用数组的形式赋值
例如:

#include <map>  
#include <string>  
#include <iostream>  
using namespace std;  
int main()  
{  
    map<int, string> mapStudent;
    mapStudent[1] = "student_one";
    mapStudent[2] = "student_two";
    mapStudent[3] = "student_three";  
    map<int, string>::iterator iter;  
    for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
        cout<<iter->first<<' '<<iter->second<<endl; 
}  

代码

#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();//m的清空
			for(int i = 1;i <= n;i++){
				double b = op[i] - ang[a] * i;
				//ax + b = y,b = y - ax
				if(!m.count(b)){//如果m里面没有b 
					m[b] = 1;
					cnt++;
				}else{
					m[b]++;
				}
			}
			if(cnt == 2){//上述循环全部遍历完,只有两个b,即两条直线 
				flag = 1;
				break;
			}
		}
		if(flag)
			puts("Yes");
		else
			puts("No");
	}
	return 0;
}

参考:
涛哥的博客https://blog.csdn.net/LinzhiQQQ/article/details/77799193?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-3.edu_weight&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-3.edu_weight.

https://blog.csdn.net/sugarbliss/article/details/81035569.

https://blog.csdn.net/qq_28351609/article/details/84630535.

猜你喜欢

转载自blog.csdn.net/Dueser/article/details/108999413
今日推荐