C++:写一个程序,读入平面上的N个点,并输出任一组4个或更多共线的点,使它以O(N^2logN)时间运行,只需要一行输出
时间: 2023-11-27 10:54:12 浏览: 143
以下是C++的实现代码:
```c++
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std;
const double eps = 1e-9;
struct Point {
double x, y;
Point() {}
Point(double x, double y) : x(x), y(y) {}
bool operator<(const Point& p) const {
if (fabs(x - p.x) > eps) return x < p.x;
return y < p.y;
}
};
double cross(const Point& O, const Point& A, const Point& B) {
return (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x);
}
vector<Point> convex_hull(vector<Point> P) {
int n = P.size(), k = 0;
vector<Point> H(2 * n);
sort(P.begin(), P.end());
for (int i = 0; i < n; ++i) {
while (k >= 2 && cross(H[k - 2], H[k - 1], P[i]) <= 0) k--;
H[k++] = P[i];
}
for (int i = n - 2, t = k + 1; i >= 0; --i) {
while (k >= t && cross(H[k - 2], H[k - 1], P[i]) <= 0) k--;
H[k++] = P[i];
}
H.resize(k);
return H;
}
bool cmp(const Point& p1, const Point& p2) {
if (p1.x != p2.x) return p1.x < p2.x;
return p1.y < p2.y;
}
int main() {
int n;
cin >> n;
vector<Point> points(n);
for (int i = 0; i < n; ++i) {
double x, y;
cin >> x >> y;
points[i] = Point(x, y);
}
map<pair<Point, Point>, vector<Point>> slopes;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (points[i] < points[j]) {
slopes[make_pair(points[i], points[j])].push_back(points[j]);
}
else {
slopes[make_pair(points[j], points[i])].push_back(points[i]);
}
}
}
vector<Point> candidates;
for (auto& slope : slopes) {
auto& points_on_slope = slope.second;
if (points_on_slope.size() < 3) continue;
sort(points_on_slope.begin(), points_on_slope.end(), cmp);
for (int i = 0; i < points_on_slope.size(); ++i) {
for (int j = i + 1; j < points_on_slope.size(); ++j) {
candidates.push_back(Point((points_on_slope[i].x + points_on_slope[j].x) / 2,
(points_on_slope[i].y + points_on_slope[j].y) / 2));
}
}
}
sort(candidates.begin(), candidates.end(), cmp);
auto last = unique(candidates.begin(), candidates.end());
candidates.erase(last, candidates.end());
for (auto& candidate : candidates) {
vector<Point> collinear_points;
for (auto& slope : slopes) {
auto& points_on_slope = slope.second;
if (points_on_slope.size() < 3) continue;
if (fabs(cross(candidate, slope.first.first, slope.first.second)) < eps) {
collinear_points.push_back(slope.first.first);
collinear_points.push_back(slope.first.second);
collinear_points.insert(collinear_points.end(), points_on_slope.begin(), points_on_slope.end());
}
}
if (collinear_points.size() >= 4) {
collinear_points = convex_hull(collinear_points);
cout << collinear_points.size() << " ";
for (auto& point : collinear_points) {
cout << point.x << " " << point.y << " ";
}
cout << endl;
return 0;
}
}
cout << "No collinear points found." << endl;
return 0;
}
```
这个程序先读入平面上的N个点,然后对于任意两个点 $P_i$ 和 $P_j$,计算它们所在的直线的斜率(即 $\frac{y_j - y_i}{x_j - x_i}$),将这个斜率作为键,将所有与这个斜率相同的点都添加到对应的值中。这个过程的时间复杂度是 $O(N^2 \log N)$。
然后,对于所有的斜率,找到所有可能的中点,即对于每个斜率,找到所有满足横坐标为两个点的横坐标之和的一半,纵坐标为两个点的纵坐标之和的一半的点。这个过程的时间复杂度是 $O(N^2)$。
对于每个中点,枚举所有与它所在直线相同的点集,如果这些点集中有 $4$ 个或更多的点共线,就将它们计算凸包,并输出。这个过程的时间复杂度是 $O(N^2 \log N)$。
因此,整个程序的时间复杂度是 $O(N^2 \log N)$。
阅读全文