news 2026/4/16 16:28:40

(新卷,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/16 13:45:35

蓝莓基质/土壤

蓝莓喜欢酸性土壤&#xff0c;pH在4-5.5之间e 换盆的时候可以加些松针土、泥炭土与原先的土1:1混合。也可以用硫酸亚铁拌土&#xff0c;100g/平方米。平时浇水的时候也可以用1升水兑上1g的硫酸亚铁&#xff0c;每10-15天浇一次。2蓝莓对氯敏感&#xff0c;平时用自来水浇水的时…

作者头像 李华
网站建设 2026/4/16 10:16:12

用Microsoft Visual Studio Installer Projects打包程序

参考https://blog.csdn.net/m0_51961114/article/details/134908822 添加文件方式 方式一&#xff1a;如下图方式&#xff0c;可能有的.dll文件没添加上 方式二&#xff1a;直接按照自己的Debug/Release下所需的文件目录和文件在Application Folder下创建并添加相关文件&…

作者头像 李华
网站建设 2026/4/16 13:36:06

【观成科技】C2框架AdaptixC2加密流量分析

工具介绍 AdaptixC2 是一款设计简洁、灵活且易于定制的命令与控制 (C2) 框架。与复杂且臃肿的大型 C2 平台不同&#xff0c;其轻量级设计使得攻击者能够更轻松地在不同环境中部署和调整。该框架采用模块化设计&#xff0c;支持C2工具的基本功能&#xff0c;例如在受感染的机器…

作者头像 李华
网站建设 2026/4/16 10:17:28

linux Page Table 和 TLB 操作总结

以下是 Linux 内核中与页表和 TLB 操作对应的主要 API/函数列表&#xff0c;结合上述操作分类&#xff1a;页表&#xff08;Page Table&#xff09;相关 API 1. 地址转换操作内核 API/函数说明虚拟地址→物理地址virt_to_phys()、__pa()内核虚拟地址转物理地址物理地址→虚拟地…

作者头像 李华
网站建设 2026/4/16 10:18:01

sparse4D v3

4个技术细节&#xff1a; temporal instance denoising quality estimation decoupled attention extend to tracking 1. Temporal Instance Denoising&#xff08;时序实例去噪&#xff09; 背景问题&#xff08;Sparse4D / v2 中的痛点&#xff09; Sparse4D 系列的核心是 …

作者头像 李华
网站建设 2026/4/16 11:58:07

Java毕设选题推荐:基于springboot的闲一品闲置品交易平台【附源码、mysql、文档、调试+代码讲解+全bao等】

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

作者头像 李华