c++ - 与平面上的点相关的问题和一些技术错误 [C++]
问题描述
问题陈述:给定平面上的 n 个点(n 是偶数)。求一对点的数量,使得通过它们的线的两侧有相同数量的点。
我正在尝试蛮力(〜O(n ^ 3)),非常感谢任何关于改进解决方案的建议。我也为同样的问题找到了这个答案,但无法实现。
主要技术问题:我已将导致错误的部分标记为// HERE
. 更准确地说,错误是:
error: no matching function for call to 'sign'
if(sign(A, B, M) == 1) right++;
error: no matching function for call to 'sign'
if(sign(A, B, M) == -1) left++;
我认为我的错误可能&
是在定义A
,B
和C
. 我该如何解决?
我的代码:
#include <iostream>
using namespace std;
// check which side of the line AB does the point M lie on
int sign(float A[2], float B[2], float M[2]) {
float det = (A[0] - M[0]) * (B[1] - M[1]) - (B[0] - M[0]) * (A[1] - M[1]);
if(det > 0) return 1;
if(det < 0) return -1;
else return 0;
}
int main() {
int n, res = 0;
cin >> n;
float points[n][2]; // array of points
// enter points via coordiantes
for(int i = 0; i < n; i++) {
for(int j = 0; j < 2; j++) {
cin >> points[i][j];
}
}
// brute force :(
for(int i = 0; i < n; i++) {
int left = 0, right = 0;
float &A = *points[i];
for(int j = 0; i < n; i++) {
if(j == i) continue;
float &B = *points[j];
for(int k = 0; k < n; k++) {
if(k == j || k == i) continue;
float &M = *points[k];
if(sign(A, B, M) == 1) right++; // HERE
if(sign(A, B, M) == -1) left++; // HERE
}
}
if(left == right) res++;
}
cout << res << endl;
return 0;
}
解决方案
我认为你不需要蛮力它,至少n^2logn
应该比 n^3 更好。我不会用 C++ 写出代码,但我会解释一下算法:
将每个点视为集线器的中心,通过线连接到所有其他点。然后具有该中心的每一对点与 x 轴形成一定的角度,您可以简单地使用 arctan(deltay/deltax) 来查找。
对于每个中心,您可以对角度进行排序 (
nlogn
),并且很容易知道O(1)
该组角度的中值角度是多少,从而知道要添加到列表中的对。这是一个极端情况,可能有几个共线对,所以不要忘记这一点。因此,如果您对 n 个点执行此操作,对 nlogn 次进行排序,您将得到解决方案
n^2logn
推荐阅读
- python - 缓存和搜索多个请求的 API 响应
- javascript - 我将尝试使用css的顺序每三天循环一次div。我该怎么办?
- java - 如何下载电子邮件附件并将其存储在下载文件夹中?
- json - 错误报告 ExtJS 7.4.0 Ext.data.reader.Json
- ionic-framework - 错误:@capacitor-community/native-audio - 缺少资产路径 [IONIC 5]
- python - python中的双x轴:相同的数据,不同的比例
- acumatica - 使用 PDF 预览创建新的审批屏幕
- python - 调用时单独运行组内的 Python Click 命令
- git - 获取与特定提交哈希关联的所有分支名称
- python-3.x - AWS Glue ETL 作业中的 Boto3 Glue