求助全WA了,玄关

P1102 A-B 数对

用下map就行~~qwp~~
by huta0 @ 2023-11-04 17:41:23


@[WangYao0126](/user/815011) ``` #include<bits/stdc++.h> using namespace std; const int maxn=1e6+10; int a[maxn],n; int c; int fnd_first(int x){ int l=0,r=n+1; while(l+1<r){ int mid=(l+r)>>1; if(a[mid]<=x) l=mid; else r=mid; } return l; } int fnd_last(int x){ int l=0,r=n+1; while(l+1<r){ int mid=(l+r)>>1; if(a[mid]<x) l=mid; else r=mid; } return l; } int main(){ ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); cin>>n>>c; for(int i=1;i<=n;i++) cin>>a[i]; sort(a+1,a+n+1); int ans=0; for(int i=1;i<=n;i++){ ans+=fnd_first(a[i]+c)-fnd_last(a[i]+c); } cout<<ans<<endl; return 0; } ``` 改了改二分和计算部分,看看行不行
by skyskyCCC @ 2023-11-04 17:58:14


@[skyskyCCC](/user/541739) 万分感谢!!!
by WangYao0126 @ 2023-11-04 18:14:00


@[WangYao0126](/user/815011) 不客气
by skyskyCCC @ 2023-11-04 18:16:56


@[WangYao0126](/user/815011) 注意ans要开long long否则第三个点会错
by skyskyCCC @ 2023-11-04 18:31:35


@[WangYao0126](/user/815011) 其实您原先的代码中的二分中的判断中大于小于号写反了
by skyskyCCC @ 2023-11-04 18:41:30


@[WangYao0126](/user/815011) 这份就只改了改二分,应该仍然可以过 ``` #include<bits/stdc++.h> using namespace std; const int maxn=1e6+10; int a[maxn],n; int c; int fnd_first(int x){ int l=1,r=n; while(l<r){ int mid=(l+r)/2; if(a[mid]>=x) r=mid; else l=mid+1; } return r; } int fnd_last(int x){ int l=1,r=n; while(l<=r){ int mid=(l+r)/2; if(a[mid]<=x) l=mid+1; else r=mid-1; } return r; } int main(){ ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); cin>>n>>c; for(int i=1;i<=n;i++) cin>>a[i]; sort(a+1,a+n+1); long long ans=0; for(int i=1;i<=n;i++){ int A=a[i]; int l=fnd_first(A-c); int r=fnd_last(A-c); if(a[l]==A-c) ans+=(r-l+1); } cout<<ans<<endl; return 0; } ```
by skyskyCCC @ 2023-11-04 18:43:52


|