P1102 A-B 数对
作者:互联网
#include<map>
#include<iostream>
#include<vector>
using namespace std;
map<int,int> m;
vector <int> ma;
long long ans=0;
int main()
{
int N,C;
cin>>N>>C;
for (int i=0;i<N;i++)
{
int k;
cin>>k;
if (m[k]==0)
{
ma.push_back(k);
}
m[k]++;
}
for (int i=0;i<ma.size();i++)
{
ans+=(long long)m[ma[i]]*m[ma[i]+C];
}
cout<<ans<<endl;
return 0;
}
方法二
//P2421 A-B数对(增强版)
#include<iostream>
#include<algorithm>
using namespace std;
int a[100001];
int main()
{
int n,c;
cin>>n>>c;
for (int i=0;i<n;i++)
{
cin>>a[i];
}
long long ans=0;
for (int i=0;i<n;i++)
{
long long s1=lower_bound(a,a+n,a[i]+c)-a;
long long s2=upper_bound(a,a+n,a[i]+c)-a;
ans=ans+s2-s1;
}
cout<<ans<<endl;
}
标签:std,main,P1102,int,数对,long,include 来源: https://www.cnblogs.com/smghj/p/15962844.html