@[Zemu_Ooo](/user/467824) 我猜你看我贴了
by lby_commandBlock @ 2024-01-20 11:50:06
```c++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 2e5 + 9;
const long long INF = numeric_limits<int>::max();
int n, c, a[N], ans;
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> c;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) {
int o = a[i] + c;
int l = 1, r = n;
while (a[l] < o && l < n) l++;//
while (a[r] > o && r > 0) r--;//
if (a[l] == o && a[r] == o) {
ans += r - l + 1;
}
}
cout << ans << endl;
return 0;
}
```
这样可以不re
by chenyuchen_1 @ 2024-01-20 11:51:07
@[lby_commandBlock](/user/791222)
by chenyuchen_1 @ 2024-01-20 11:51:15
谢谢 @[chenyuchen_1](/user/1023780) ,但是关于你的格言。。。
`> 最后在线时间:2024年1月6日22时5分 < 由 exOIso 发送激光`
by lby_commandBlock @ 2024-01-20 11:52:33
@[lby_commandBlock](/user/791222) 看起来是双指针写的。首先双指针好像没啥问题,但是你在这道题这么用,逻辑就变成了通过二分查找找到满足 $a[i] + C = a[j]$ 的 $a[j]$。这就代表对于每个元素你都在做一次二分查找,复杂度是 $O(N \log N)$,反正挺大的(
我不太清楚 RE 是啥问题,因为我很讨厌改 RE 的点(大哭,但是我推测可能是当 $a[i] + c$ 的值超出了数组索引范围或者不存在于数组中时,你的循环将会继续执行直到 $l$ 或 $r$ 越界,从而可能导致访问非法内存。
所以我直接干脆改成哈希表存了,直接查询 $A = B + C$ 中的 $A$ 是否存在和存在的次数,快多了,反正一遍就过了(
```cpp
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 9;
int n, c, a[N], ans;
unordered_map<int, int> freq;
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> c;
for (int i = 1; i <= n; i++) {
cin >> a[i];
freq[a[i]]++;
}
for (int i = 1; i <= n; i++) {
int b = a[i] - c;
if (freq.count(b)) {
ans += freq[b];
}
}
cout << ans << endl;
return 0;
}
```
by Zemu_Ooo @ 2024-01-20 11:53:33
@[lby_commandBlock](/user/791222) 没开而已,最近关了OIso
by chenyuchen_1 @ 2024-01-20 13:26:57