AC11 TLE1 求大佬帮忙看代码
查看原帖
AC11 TLE1 求大佬帮忙看代码
490461
封禁用户楼主2021/9/12 15:30

https://www.luogu.com.cn/record/57847346

#include<bits/stdc++.h>
#define rg register
using namespace std;
int a[(int)1e7];int ans=0;
int main(){
    int n,c;
    scanf("%d %d",&n,&c);
    //ios::sync_with_stdio(false);
    for (rg int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    sort(a+1,a+1+n);
    for (rg int i=1;i<=n;i++){
        int b=a[i]-c;
        rg int l=1,r=n;
        while (l<=r){
            int mid=(l+r)/2;
            if (b<=a[mid]) r=mid-1;
            else l=mid+1;
        }
        for (rg int j=l;;j++){
            if (a[j]!=b) break;
            else ans++;
        }
    }
    cout<<ans;
    return 0;
}
2021/9/12 15:30
加载中...