暴力过不去,去学双指针
by Hughpig @ 2023-09-22 21:10:38
@[mzl1234](/user/1049483) 可以用 `[upper/lower]_bound`
by Carroty_cat @ 2023-09-22 21:18:44
建议用双指针,暴力不好过,双指针代码写好了
```cpp
//为啥不用双指针?
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const LL N=2e5+10086;
LL a[N];
LL ans=0;
int main(){
LL n;
LL c;
scanf("%lld%lld",&n,&c);
for(LL i=0;i<n;i++)scanf("%lld",&a[i]);
LL A=1,B=0;
sort(a,a+n);
for(;B<n&&A<n;)//A,B指针都只向前走
{
while(a[A]-a[B]<c&&A<n)
{
A++;
}
if(a[A]-a[B]==c)
{
LL i=1,j=1;//没有重复的数字就是ans++
while(a[++B]==a[B-1])//记录重复的数字,和上一位一样就多一种
{
i++;
}
while(a[++A]==a[A-1])
{
j++;
}
ans+=i*j;
}
while(a[A]-a[B]>c)
{
B++;
}
}
printf("%lld\n",ans);
return 0;
}
```
by wcy123bc @ 2023-09-22 21:24:51
@[Hughpig](/user/646208) 谢谢
by MZL1234 @ 2023-09-22 21:27:58
@[wcy123bc](/user/1047764) c++没学太长时间,谢谢
by MZL1234 @ 2023-09-22 21:28:55