![](/img/trans.png)
[英]C++: I don't know how to solve the timeout in the problem of comparing coordinates in the coordinate plane
[英]C++) I don't know the fast algorithm to compare coordinates
最多可輸入 100,000 個坐標。 只輸出對應特定條件的坐標。 如果存在x值比每個坐標大而y值小的坐標,則對應的坐標從輸出列表中排除。
我的英語不好,所以我舉了一些例子。
[輸入] 首先輸入要輸入的坐標數N。 並輸入坐標。
[輸出] 條件對應的坐標編號按升序輸出。
[input example]
6
1 3
6 6
7 3
8 2
8 6
2 1
[output example]
4
5
6
下面的問題用一個簡單的循環就解決了,但是輸入100000個值就會超時。 我不知道該使用哪種算法。
我還附上了我寫的 C++ 源代碼。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
bool* visible = new bool[N];
for (int i = 0; i < N; i++)visible[i] = true;
vector<pair<int,pair<int, int>>> v;
for (int i = 0; i < N; i++) {
int a, b;
cin >> a >> b;
v.push_back(make_pair(i,make_pair(a, b)));
}
for (int i = 0; i < v.size(); i++) {
if (visible[i] == false)
continue;
for (int j = 0; j < v.size(); j++) {
if (visible[i] == true &&visible[j]==true && v[i].second.first < v[j].second.first && v[i].second.second > v[j].second.second) {
visible[i] = false;
break;
}
else if (visible[i] == true && visible[j] == true && v[i].second.first > v[j].second.first && v[i].second.second < v[j].second.second) {
visible[j] = false;
continue;
}
}
}
for (int i = 0; i < v.size(); i++) {
if (visible[i] == true)
cout << v[i].first + 1 << endl;
}
return 0;
}
看起來像關於控制點集的問題。
按 X 值排序點(在 X 相等的情況下,Y 是輔助鍵)
將第一個點(具有最大 X)分配給Big
變量並將其添加到結果中
遍歷數組。 如果您遇到不受Big
支配的點 - 將其分配給Big
並將其添加到結果中
您的問題的正確術語是帕累托前沿(ier)、ND 樹、KD 樹、非優勢問題。
有一些圖書館,例如https://github.com/alandefreitas/pareto-front
然而它 2 變暗這是一項簡單的任務。 您必須按坐標之一對點進行排序。 然后從最好的一個到最壞的一個掃描,只有當另一個坐標不是由先前獲取的點支配時才接受。 它會給你O(n log(n))
,這是此類任務的一般情況下最好的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.