28| A-B数对

代码实现

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N = 2e5 + 10; 
unordered_map <int, int> a;
int cnt, n, c, b[N];

signed main()
{
	cin >>  n >> c;
	for (int i = 1; i <= n; i++) 
	{
		cin >> b[i];
		a[b[i]]++;
	}
	for (int i = 1; i <= n; i++)
	{
		cnt += a[b[i] + c];
	}
	cout << cnt << endl;
	return 0;
}
cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N = 2e5 + 10; 
LL cnt, n, c, b[N];

int main()
{
	cin >>  n >> c;
	for (int i = 1; i <= n; i++) cin >> b[i];
	// 预处理 有序 
	sort(b+1, b+n+1);
	for (int i = 1; i <= n; i++)
	{
		LL x = b[i] + c;
		auto l = lower_bound(b + 1, b + n + 1, x);
		auto r = upper_bound(b + 1, b + n + 1, x); 
		cnt += (r - l);
	}
	cout << cnt << endl;
	return 0;
}
相关推荐
Desirediscipline1 小时前
#include<limits>#include <string>#include <sstream>#include <iomanip>
java·开发语言·前端·javascript·算法
The_Uniform_C@t21 小时前
AWD | ATT&CK实战系列--蓝队防御(一)
网络·学习·网络安全
摇滚侠1 小时前
RocketMQ 教程丨深度掌握 MQ 消息中间件,笔记 39-44
笔记·rocketmq
Felven1 小时前
B. Luntik and Subsequences
算法
菜鸡儿齐2 小时前
leetcode-括号生成
算法·leetcode·职场和发展
月下雨(Moonlit Rain)2 小时前
数据库笔记
数据库·笔记
fs哆哆2 小时前
在VB.NET中,随机打乱列表顺序的算法与方法
算法·.net
希望之晨2 小时前
c++ 11 学习 函数模板
linux·开发语言·c++
pen-ai2 小时前
【Yolo系列】Yolov3 目标检测算法原理详解
算法·yolo·目标检测