事件推送
标题:事件推送 | 时间限制:1秒 | 内存限制:262144K | 语言限制:不限
同一个数轴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
1)Ai <= Bj
2)Ai, Bj之间的距离小于等于R
3)在满足1)2)的情况下,每个Ai只需输出距离最近的Bj
4)输出结果按Ai从小到大的顺序排序
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String params = scanner.nextLine(); String mLine = scanner.nextLine(); String nLine = scanner.nextLine(); String[] paramsStrList = params.split(" "); int R = Integer.valueOf(paramsStrList[2]); String[] mStrList = mLine.split(" "); String[] nStrList = nLine.split(" "); int nStartIndex = 0; for (int i = 0; i < mStrList.length; i++) { if (mStrList[i].trim().length() <= 0) { continue; } int mValue = Integer.valueOf(mStrList[i]); for (int j = nStartIndex; j < nStrList.length; j++) { nStartIndex = j; if (nStrList[j].trim().length() <= 0) { continue; } int nValue = Integer.valueOf(nStrList[j]); if (mValue <= nValue) { if (nValue - mValue <= R) { System.out.println(mValue + " " + nValue); } break; } } } } }
while True: try: m, n, dis = map(int, input().split()) set_a = list(map(int, input().split())) set_b = list(map(int, input().split())) i, j = 0, 0 temp = list() while True: if i >= m or j >= n: break if set_b[j] - set_a[i] < 0: j += 1 else: if set_b[j] - set_a[i] <= dis: temp.append((set_a[i], set_b[j])) i += 1 for i, j in temp: print(i, j) except: break
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int sumA = in.nextInt(); int sumB = in.nextInt(); int r = in.nextInt(); int[] a = new int[sumA]; int[] b = new int[sumB]; for(int i=0;i<sumA;i++){ a[i] = in.nextInt(); } for(int i=0;i<sumB;i++){ b[i] = in.nextInt(); } StringBuilder result = new StringBuilder(); int i = 0,j = 0; while(i<sumA){ if(a[i]>b[sumB-1]){ break; } while(a[i]>b[j]){ j++; } if(b[j]-a[i]<=r){ result.append(a[i]+" "+b[j]+"\n"); } i++; } System.out.println(result); } } }