Advertisement
medon3

Untitled

Oct 16th, 2022
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. public class SafeHouses {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. Scanner scanner = new Scanner(System.in);
  11.  
  12. int n = Integer.parseInt(scanner.nextLine());
  13.  
  14. String[] numbersAsString = scanner.nextLine().split(" ");
  15. // int[] positions = new int[numbersAsString.length];
  16. List<Integer> positionsList = new ArrayList<>();
  17.  
  18. for (int i = 0; i < numbersAsString.length; i++) {
  19. // positions [i] = Integer.parseInt(numbersAsString[i]);
  20. positionsList.add(Integer.parseInt(numbersAsString[i]));
  21. }
  22.  
  23. int maxDistance = 0;
  24.  
  25. if(n==positionsList.size()){
  26. System.out.println(maxDistance);
  27. return;
  28. }
  29.  
  30. if(positionsList.size()>1){
  31. Collections.sort(positionsList);
  32. }else{
  33. if(positionsList.get(0)>=(n*1.0/2)){
  34. maxDistance = positionsList.get(0);
  35. }else{
  36. maxDistance = (n -1) - positionsList.get(0);
  37. }
  38. }
  39.  
  40. int distance = 0;
  41. // int nextPosition = 0;
  42.  
  43. for (int i = 0; i < positionsList.size()-1; i++) {
  44. int nextPosition = positionsList.get(i+1);
  45. int difference = nextPosition - positionsList.get(i);
  46.  
  47. if(i!=0){
  48. if(i+1==positionsList.size()-1){
  49. if((difference * 1.0/2)> ((n-1)- nextPosition)){
  50. if(difference%2==0){
  51. distance = difference/2;
  52. }else{
  53. distance = (difference - 1)/2;
  54. }
  55. }else{
  56. distance = (n-1)- nextPosition;
  57. }
  58. }else{
  59. if(difference%2==0){
  60. distance = difference/2;
  61. }else{
  62. distance = (difference - 1)/2;
  63. }
  64. }
  65. }else{
  66. if((difference * 1.0/2)>positionsList.get(i)){
  67. if(i+1==positionsList.size()-1){
  68. if((difference * 1.0/2)> ((n-1)- nextPosition)){
  69. if(difference%2==0){
  70. distance = difference/2;
  71. }else{
  72. distance = (difference - 1)/2;
  73. }
  74. }else{
  75. distance = (n-1)- nextPosition;
  76. }
  77. }else{
  78. if(difference%2==0){
  79. distance = difference/2;
  80. }else{
  81. distance = (difference - 1)/2;
  82. }
  83. }
  84. }else{
  85. distance = positionsList.get(i);
  86. }
  87. }
  88.  
  89. if(distance>maxDistance){
  90. maxDistance = distance;
  91. }
  92.  
  93.  
  94. }
  95.  
  96. System.out.println(maxDistance);
  97. }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement