其它题目没有AC就不贴了
2.
import java.util.Arrays; import java.util.Scanner; /* 100% */ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); int[] nums = new int[m]; for (int i = 0; i < m; i++) { int x = in.nextInt(); int y = in.nextInt(); if (y == 0) { nums[i] = x; } else if (x == n) { nums[i] = x + y; } else if (y == n) { nums[i] = (n - x) + 2 * n; } else if (x == 0) { nums[i] = (n - y) + 3 * n; } } Arrays.sort(nums); int res = Integer.MAX_VALUE; k--; for (int i = 0; i < nums.length; i++) { if (i + k < nums.length) { res = Math.min(nums[i + k] - nums[i], res); } else { res = Math.min(4 * n - nums[i] + nums[(i + k) % nums.length], res); } } System.out.println(res); } }
全部评论
(1) 回帖