news 2026/7/14 10:01:16

(新卷,100分)- 事件推送(Java JS Python C)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
(新卷,100分)- 事件推送(Java JS Python C)

(新卷,100分)- 事件推送(Java & JS & Python & C)

题目描述

同一个数轴X上有两个点的集合A={A1, A2, …, Am}和B={B1, B2, …, Bn},Ai和Bj均为正整数,A、B已经按照从小到大排好序,

A、B均不为空,给定一个距离R(正整数),列出同时满足如下条件的所有(Ai, Bj)数对:

  1. Ai <= Bj
  2. Ai, Bj之间的距离小于等于R
  3. 在满足1,2的情况下,每个Ai只需输出距离最近的Bj
  4. 输出结果按Ai从小到大的顺序排序
输入描述

第一行三个正整数m,n,R

第二行m个正整数,表示集合A

第三行n个正整数,表示集合B

输入限制:

1<=R<=100000, 1<=n,m<=100000, 1<=Ai,Bj<=1000000000

输出描述

每组数对输出一行Ai和Bj,以空格隔开

用例
输入4 5 5
1 5 5 10
1 3 8 8 20
输出1 1
5 8
5 8
说明
题目解析

本题数量级非常大,因此使用双重for是肯定会超时的。

我的解题思路是利用二分查找。

关于标准二分查找的实现可以参考Java的Arrays.binarySearch

中关于binarySearch的具体实现,特别是其中关于有序插入位置的实现原理。

本题,我们只需要遍历每一个a[i],然后通过二分查找去找他们在b中的位置 j :

  • 如果 j >= 0,则说明b数组中有一个b[j] == a[i],此时a[i] b[j]就是符合要求的组合
  • 如果 j < 0,则说明b数组中没有b[j] == a[i],此时 j 其实就是 - insertIdx - 1,其中insertIdx就是a[i]在b中的有序插入位置,因此b[insertIdx] > a[i],如果 b[insertIdx] - a[i] <= r,那么a[i] b[insertIdx]就是符合要求的组合

上面算法的事件复杂度只有O(nlogn),不会超时。


找到的有序插入位置可能 insertIdx == b.length,即使越界位置,此情况b数组中不存在比a[i]大的值


本题最优策略为同向双指针,可达O(n)时间复杂度

二分查找解法

Java算法源码
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Integer[] tmp = Arrays.stream(sc.nextLine().split(" ")).map(Integer::parseInt).toArray(Integer[]::new); int m = tmp[0]; int n = tmp[1]; int r = tmp[2]; Integer[] a = Arrays.stream(sc.nextLine().split(" ")).map(Integer::parseInt).toArray(Integer[]::new); Integer[] b = Arrays.stream(sc.nextLine().split(" ")).map(Integer::parseInt).toArray(Integer[]::new); getResult(r, a, b); } public static void getResult(int r, Integer[] a, Integer[] b) { for (int ai : a) { int j = Arrays.binarySearch(b, ai); // 如果在b数组中可以找到ai,则此时j就是ai在b数组中的位置,此时ai和bj是满足要求,且最接近的 if (j >= 0) { System.out.println(ai + " " + b[j]); } else { // 如果在b数组中找不到ai,则此时j就是ai在b数组中有序插入位置-insertIdx-1, // 因此insertIdx = -j-1,此时b[insertIdx]满足大于ai,我们只需要检查b[insertIdx] - ai<=r即可 int insertIdx = -j - 1; // 有序插入位置可能是越界位置 if (insertIdx == b.length) continue; if (b[insertIdx] - ai <= r) System.out.println(ai + " " + b[insertIdx]); } } } }
JS算法源码
/* JavaScript Node ACM模式 控制台输入获取 */ const { userInfo } = require("os"); const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const lines = []; rl.on("line", (line) => { lines.push(line); if (lines.length === 3) { let [m, n, r] = lines[0].split(" ").map((ele) => parseInt(ele)); let a = lines[1].split(" ").map(Number); let b = lines[2].split(" ").map(Number); getResult(a, b, r); lines.length = 0; } }); function getResult(a, b, r) { for (let ai of a) { const j = binarySearch(b, ai); if (j >= 0) { console.log(`${ai} ${b[j]}`); } else { const insertIdx = -j - 1; if (insertIdx == b.length) continue; if (b[insertIdx] - ai <= r) console.log(`${ai} ${b[insertIdx]}`); } } } function binarySearch(arr, target) { let low = 0; let high = arr.length - 1; while (low <= high) { const mid = (low + high) >> 1; const midVal = arr[mid]; if (midVal > target) { high = mid - 1; } else if (midVal < target) { low = mid + 1; } else { return mid; } } return -low - 1; }
Python算法源码
# 输入获取 m, n, r = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) def binarySearch(arr, target): low = 0 high = len(arr) - 1 while low <= high: mid = (low + high) >> 1 midVal = arr[mid] if midVal > target: high = mid - 1 elif midVal < target: low = mid + 1 else: return mid return -low - 1 # 算法入口 def getResult(): for ai in a: j = binarySearch(b, ai) if j >= 0: print(f"{ai} {b[j]}") else: insertIdx = -j - 1 if insertIdx == len(b): continue if b[insertIdx] - ai <= r: print(f"{ai} {b[insertIdx]}") # 算法调用 getResult()
C算法源码
#include <stdio.h> int binarySearch(const int nums[], int nums_size, int key) { int low = 0; int high = nums_size - 1; while(low <= high) { int mid = (low + high) >> 1; int midVal = nums[mid]; if(midVal > key) { high = mid - 1; } else if(midVal < key) { low = mid + 1; } else { return mid; } } return -low - 1; } int main() { int m, n, r; scanf("%d %d %d", &m, &n, &r); int a[m]; for(int i=0; i<m; i++) { scanf("%d", &a[i]); } int b[n]; for(int i=0; i<n; i++) { scanf("%d", &b[i]); } for(int i=0; i<m; i++) { int j = binarySearch(b, n, a[i]); if(j >= 0) { printf("%d %d\n", a[i], b[j]); } else { int insertIdx = -j - 1; if(insertIdx == n) { continue; } if(b[insertIdx] - a[i] <= r) { printf("%d %d\n", a[i], b[insertIdx]); } } } return 0; }

同向双指针解法

Java算法源码
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Integer[] tmp = Arrays.stream(sc.nextLine().split(" ")).map(Integer::parseInt).toArray(Integer[]::new); int m = tmp[0]; int n = tmp[1]; int r = tmp[2]; int[] a = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray(); int[] b = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray(); getResult(r, a, b); } public static void getResult(int r, int[] a, int[] b) { int i = 0; int j = 0; while (i < a.length && j < b.length) { if (a[i] <= b[j]) { if (b[j] - a[i] <= r) System.out.println(a[i] + " " + b[j]); i++; } else { j++; } } } }
JS算法源码
/* JavaScript Node ACM模式 控制台输入获取 */ const { userInfo } = require("os"); const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const lines = []; rl.on("line", (line) => { lines.push(line); if (lines.length === 3) { let [m, n, r] = lines[0].split(" ").map((ele) => parseInt(ele)); let a = lines[1].split(" ").map(Number); let b = lines[2].split(" ").map(Number); getResult(a, b, r); lines.length = 0; } }); function getResult(a, b, r) { let i = 0; let j = 0; while (i < a.length && j < b.length) { if (a[i] <= b[j]) { if (b[j] - a[i] <= r) console.log(a[i] + " " + b[j]); i++; } else { j++; } } }
Python算法源码
# 输入获取 m, n, r = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) # 算法入口 def getResult(): i = 0 j = 0 while i < len(a) and j < len(b): if a[i] <= b[j]: if b[j] - a[i] <= r: print(f"{a[i]} {b[j]}") i += 1 else: j += 1 # 算法调用 getResult()
C算法源码
#include <stdio.h> int main() { int m, n, r; scanf("%d %d %d", &m, &n, &r); int a[m]; for(int i=0; i<m; i++) { scanf("%d", &a[i]); } int b[n]; for(int i=0; i<n; i++) { scanf("%d", &b[i]); } int i = 0; int j = 0; while(i < m && j < n) { if(a[i] <= b[j]) { if(b[j] - a[i] <= r) { printf("%d %d\n", a[i], b[j]); } i++; } else { j++; } } return 0; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/1 9:36:08

制造业七大核心系统盘点——ERP、MES、WMS、SCM、PLM、SCADA、QMS

我这几年跑工厂、聊老板、跟信息化负责人沟通&#xff0c;听到最多的一句话不是我们没系统&#xff0c;而是&#xff1a;ERP 上了&#xff0c;车间还是一团乱MES 买了&#xff0c;数据没人信仓库有系统&#xff0c;库存还是对不上系统一堆&#xff0c;但问题一个没少这时候很多…

作者头像 李华
网站建设 2026/7/12 14:41:43

前端使用docker打包nuxt官网项目

安装docker的文章在另一篇&#xff1a;https://blog.csdn.net/m0_69727853/article/details/154741168?spm1001.2014.3001.5501 1. 查看docker是否安装成功 docker -v 2. 如果显示没有docker&#xff0c;查看当前的环境变量是否正确 tips提示&#xff1a;如果找不到安装的doc…

作者头像 李华
网站建设 2026/7/1 16:05:34

Java计算机毕设之基于Java+springboot的寿险公司人力资源管理系统基于SpringBoot的人力资源管理系统(完整前后端代码+说明文档+LW,调试定制等)

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华
网站建设 2026/7/1 9:36:14

PolarDB-X 企业版分布式集群部署文档

目录PolarDB-X 企业版分布式集群部署文档快速连接快速连接命令集群信息集群状态Pod 列表服务列表镜像版本安装步骤1. 安装 Helm2. 创建命名空间3. 添加 Helm 仓库并安装 Operator4. 获取最新镜像版本5. 创建集群配置文件6. 部署集群7. 监控部署进度8. 获取连接密码集群架构架构…

作者头像 李华
网站建设 2026/7/1 9:36:14

计算机Java毕设实战-基于springboot的社区协作与资源共享系统社区闲置资源交易与共享系统【完整源码+LW+部署说明+演示视频,全bao一条龙等】

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华