4 solutions
-
1
#include<bits/stdc++.h> using namespace std; const int N=1e7+5; int a[N]; int b[N]; int n;//以下是两个自己常用版子, int find(int x){ //求右边界,若求大于某个数的第一个位置就在return r+1 int l=0,r=n-1; while(l<=r){ int mid=l+r>>1; if(a[mid]>x){ r=mid-1; } else{ l=mid+1; } } return r; } int find1(int x){//求小于某个数的第一个位置,同上理若求左边界就只需ruturn l int l=0,r=n-1; while(l<=r){ int mid=l+r>>1; if(a[mid]>=x){ r=mid-1; } else{ l=mid+1; } } return l-1; } int main(){ int x; cin>>n>>x; for(int i=0;i<n;i++){ cin>>a[i]; } sort(a,a+n); int num; int ans=0; for(int i=0;i<n;i++){ num=a[i]+x; ans+=find(num)-find1(num); } cout<<ans; return 0; }
Information
- ID
- 279
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 8
- Tags
- # Submissions
- 297
- Accepted
- 57
- Uploaded By