Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Oprst {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int n = in.nextInt(), k = in.nextInt();
- int[] a = new int[n], b = new int[k];
- for (int i = 0; i < n; i++) {
- a[i] = in.nextInt();
- }
- for (int i = 0; i < k; i++) {
- b[i] = in.nextInt();
- int left = 0, right = n - 1, middle;
- while (left <= right) {
- middle = (right + left) / 2;
- if (a[middle] <= b[i])
- left = middle + 1;
- else
- right = middle - 1;
- }
- if (right < 0)
- right = left;
- if (left >= n)
- left = right;
- if (Math.abs(b[i] - a[left]) < Math.abs(a[right] - b[i]))
- System.out.println(a[left]);
- else
- System.out.println(a[right]);
- }
- in.close();
- }
- }
Add Comment
Please, Sign In to add comment