用哈希表查找为 $O(1)$。
这样的话不会 TLE。
```cpp
#include <iostream>
#include <unordered_map>
#define int long long
using namespace std;
signed main() {
int n, c;
cin >> n >> c;
unordered_map<int, int> frequency;
for (int i = 0; i < n; i++) {
int num;
cin >> num;
frequency[num]++;
}
int countPairs = 0;
for (auto it = frequency.begin(); it != frequency.end(); ++it) {
int num = it->first;
int freq = it->second;
int complement = num - c;
if (frequency.find(complement) != frequency.end()) {
countPairs += freq * frequency[complement];
}
}
cout << countPairs << endl;
return 0;
}
```
by Linear_Function @ 2024-04-24 23:15:55
@[QwQ_77](/user/1055521) 记得关注!
by Linear_Function @ 2024-04-24 23:16:37
@[Linear_Function](/user/970052)
蟹蟹!!
已关
by QwQ_77 @ 2024-04-25 18:46:11
谢谢
by Linear_Function @ 2024-04-25 18:47:31