求指教,58分,模拟几遍都找不出问题

P1102 A-B 数对

你看一下这组数据: 5 1 1 1 2 3 3 应输出4,你输出6
by chenyushuo123 @ 2023-09-17 16:35:41


```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; } ``` 你A往后找时没限定A<n,因为a数组默认为0,0-a[b]肯定<c,所以你的A直接就窜出去了,干到200000多
by chenyushuo123 @ 2023-09-17 16:46:23


@[chenyushuo123](/user/508718) 是的好像是边界没处理好,感谢
by hhhhaa @ 2023-09-17 17:07:02


@[chenyushuo123](/user/508718) 哈哈哈明白啦,tql
by hhhhaa @ 2023-09-17 17:08:13


|