lower_bound与upper_bound
作者:互联网
一句话题意:给一串数和正整数 \(C\) ,求串中 \(A - B = C\) 的数对个数
做法:排序,对每个 \(a_i\) ,找upper_bound - lower_bound即是 \(a_i - C\) 的个数
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, c, a[N];
int main(){
scanf("%d%d", &n, &c);
for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
sort(a + 1, a + n + 1);
long long ans = 0;
for(int i = 1; i <= n; ++i) ans += (upper_bound(a + 1, a + n + 1, a[i] + c) - a - (lower_bound(a + 1, a + n + 1, a[i] + c) - a));
cout << ans;
return 0;
}
标签:upper,lower,int,d%,个数,bound 来源: https://www.cnblogs.com/fakeryu/p/16120949.html