news 2026/5/3 19:18:56

(新卷,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/4/30 8:17:47

Flutter for HarmonyOS 开发指南(二):Hello World

一&#xff1a;零基础快速入门Dart Flutter开发 这段代码是 Flutter 官方提供的标准“计数器”示例&#xff0c;也是学习 Flutter 的“Hello World”。现对它进行了一些修改&#xff08;添加了全局主题配置&#xff09;。 效果&#xff1a; 二&#xff1a;示例代码 main.dar…

作者头像 李华
网站建设 2026/5/3 19:12:49

sparse4D V2核心要点

这个图是sparseV2的结构&#xff0c;单帧网络输出的instance和历史帧的instance是如何在多帧网络里融合的&#xff1f;因为单帧网络基于当前img检出的结果肯定跟历史帧是有重叠的&#xff0c;初读文章的疑问是&#xff1a;如何把重合的这部分一一对应上呢一句话先给结论&#x…

作者头像 李华
网站建设 2026/4/28 20:19:11

学霸同款2026 AI论文工具TOP9:自考毕业论文全攻略

学霸同款2026 AI论文工具TOP9&#xff1a;自考毕业论文全攻略 2026年自考论文写作工具测评&#xff1a;为何需要这份榜单&#xff1f; 随着人工智能技术的不断进步&#xff0c;AI论文工具逐渐成为学术写作的重要辅助手段。对于自考学生而言&#xff0c;撰写毕业论文不仅是学业的…

作者头像 李华