全网首个不用map的解法!!!
2024-06-28 18:08:13
发布于:广东
51阅读
0回复
0点赞
为了改bug我还花了一次机会看测试点😭
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
//#include <map>
using namespace std;
int a[200005], b[200005], bucket[200005], ctb;
int find(int left, int right, int val, int *a){//分治查找
if(a[left] == val) return left;
if(a[right] == val) return right;
if(a[left] > val || a[right] < val) return 0;
int mid = (left + right) >> 1;
return max(find(left, mid, val, a), find(mid + 1, right, val, a));
}
int read(){//快读
char c = getchar();
int x = 0;
while(!isdigit(c)) c = getchar();
while(isdigit(c)){
x = (x << 3) + (x << 1) + c - '0';
c = getchar();
}return x;
}
int main(){
b[0] = -1;
int n = read(), m = read();
for(int i = 1; i <= n; i++){
a[i] = read();
}sort(a + 1, a + n + 1);//sort
for(int i = 1; i <= n; i++){
if(b[ctb] != a[i]) b[++ctb] = a[i];//记录有多少种数字
}
for(int i = 1; i <= n; i++){
bucket[find(1, ctb, a[i], b)]++;//记录每种数字的个数
}
long long ct = 0;
for(int i = 1; i <= ctb; i++){
if(b[i] >= m){
int idx = find(1, ctb, b[i] - m, b);
ct += bucket[idx] * 1ll * bucket[i];//计算
}
}
cout << ct;
return 0;
}
时间复杂度:
这里空空如也
有帮助,赞一个