P1102 A-B数对
P1102 A-B数对
用map过掉,可以当高效的桶排用,map<long long,int>m;
意思是m[long long]==int;
#include<iostream> #include<cstdio> #include<queue> #include<algorithm> #include<cmath> #include<map> #include<ctime> #include<cstring> #define inf 2147483647 #define For(i,a,b) for(register long long i=a;i<=b;i++) #define p(a) putchar(a) #define g() getchar() //by war //2017.10.13 using namespace std; long long n,c,ans; long long a[200010]; map<long long,int>m; void in(long long &x) { long long y=1; char c=g();x=0; while(c<'0'||c>'9') { if(c=='-') y=-1; c=g(); } while(c<='9'&&c>='0')x=x*10+c-'0',c=g(); x*=y; } void o(long long x) { if(x<0) { p('-'); x=-x; } if(x>9)o(x/10); p(x%10+'0'); } long long find(long long x) { long long l=x,r=n,mid; while(l<r) { mid=(l+r)>>1; if(a[mid]>=a[x]+c) r=mid; else l=mid+1; } return l-1; } int main() { in(n),in(c); For(i,1,n) in(a[i]),m[a[i]]++; sort(a+1,a+n+1); For(i,1,n) ans+=m[a[i]-c]; o(ans); return 0; }