fork download
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. long L = scanner.nextLong();
  9. int N = scanner.nextInt();
  10. int K = scanner.nextInt();
  11. long[] A = new long[N];
  12. for (int i = 0; i < N; i++) {
  13. A[i] = scanner.nextLong();
  14. }
  15.  
  16. int countOutput = 0;
  17. long d = 0;
  18. while (countOutput < K) {
  19. Map<Long, Integer> pointToIndexMap = new HashMap<>();
  20. for (int i = 0; i < N; i++) {
  21. long x1 = A[i] - d;
  22. if (x1 >= 0 && x1 <= L) {
  23. pointToIndexMap.putIfAbsent(x1, i);
  24. }
  25. long x2 = A[i] + d;
  26. if (x2 >= 0 && x2 <= L) {
  27. pointToIndexMap.putIfAbsent(x2, i);
  28. }
  29. }
  30.  
  31. int countD = 0;
  32. for (Map.Entry<Long, Integer> entry : pointToIndexMap.entrySet()) {
  33. long x = entry.getKey();
  34. int idx = entry.getValue();
  35. long minDist;
  36. if (x <= A[idx]) {
  37. minDist = A[idx] - x;
  38. if (idx > 0) {
  39. minDist = Math.min(minDist, Math.abs(x - A[idx - 1]));
  40. }
  41. } else {
  42. minDist = x - A[idx];
  43. if (idx < N - 1) {
  44. minDist = Math.min(minDist, Math.abs(x - A[idx + 1]));
  45. }
  46. }
  47. if (minDist == d) {
  48. countD++;
  49. }
  50. }
  51.  
  52. for (int i = 0; i < countD && countOutput < K; i++) {
  53. System.out.println(d);
  54. countOutput++;
  55. }
  56. d++;
  57. }
  58. }
  59. }
Success #stdin #stdout 0.15s 54628KB
stdin
7 1 4
3
stdout
0
1
1
2