题意:给出一些点,要求有一对平行的直线包含所有这些点,问是否有解。
解法:当时写的是n^2的暴力,但是现在发现并不需要这么麻烦。只需要利用鸽巢原理的性质,枚举前三个点两两匹配共三种情况,看是否有解即可。
代码如下:
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<stack>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<map>
using namespace std;
const int maxn = 1005;
const double eps = 1e-6;
int n;
int x[maxn], y[maxn];
vector <int> v;
double Count(int a, int b) {
int x1 = x[a], x2 = x[b], y1 = y[a], y2 = y[b];
return (double)(y1 - y2) / (x1 - x2);
}
bool solve(int a, int b) {
double k = Count(a, b);
int cnt = 1;
v.clear();
for(int i = 1; i <= n; i++) {
if(i == a)
continue;
if(abs(Count(a, i) - k) < eps)
continue;
v.push_back(i);
}
if(v.size() == 1)
return 1;
if(v.size() == 0)
return 0;
for(int i = 1; i < v.size(); i++) {
if(abs(Count(v[0], v[i]) - k) > eps)
return 0;
}
return 1;
}
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
x[i] = i;
scanf("%d", &y[i]);
}
if(solve(1, 2) || solve(1, 3) || solve(2, 3))
printf("Yes\n");
else
printf("No\n");
return 0;
}