【发布时间】:2021-02-14 23:18:53
【问题描述】:
演练公告:https://cses.fi/problemset/task/1640/
我想了解为什么一种算法比另一种算法快得多(大约 25 倍),因为我认为它们具有相同的复杂度 O(n log n),但最快的算法原则上应该在我的列表中多循环 1 次,所以我真的会认为它会慢一些。
慢:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x; cin >> n >> x;
vector<int> a(n);
map<int, int> vals;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
if(vals.count(x - a[i])){
cout << i + 1 << " " << vals[x - a[i]] << "\n";
return 0;
}
vals[a[i]] = i + 1;
}
cout << "IMPOSSIBLE" << '\n';
}
快速:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, x;
cin >> n >> x;
pair<int, int> arr[n];
for (int i = 0; i < n; i++) {
cin >>arr[i].first;
arr[i].second = i + 1;
}
sort(arr, arr + n);
int i = 0, j = n - 1;
while (i < j) {
int sum = arr[i].first + arr[j].first;
if (sum == x) {
cout << arr[i].second << ' ' << arr[j].second;
return 0;
} else if (sum < x) {
i++;
} else {
j--;
}
}
cout << "IMPOSSIBLE";
return 0;
}
【问题讨论】:
-
为什么只有第二个有
ios::sync_with_stdio(false); cin.tie(0);?您是否认为差异可能是由于这个原因? -
请提供minimal reproducible example 输入和输出以及代码应该做什么
-
我做了测试,差异要小得多,但第二个仍然快两倍。我尝试使用 unordered_map 并且我对一次测试有时间限制。我真的不明白
-
我放了一个链接,所有的东西都写好了,代码是可测试的,以了解性能,这不是一个好习惯吗?
-
在第一个代码中,您正在迭代地执行排序,这相对于第二个代码来说是次优的,其中排序是一次性执行的。第二部分只有 O(n)。
标签: c++ algorithm optimization