|
| 1 | +import java.io.BufferedOutputStream; |
| 2 | +import java.io.IOException; |
| 3 | +import java.util.List; |
| 4 | +import java.util.NavigableSet; |
| 5 | +import java.util.Scanner; |
| 6 | +import java.util.TreeMap; |
| 7 | +import java.util.TreeSet; |
| 8 | + |
| 9 | +public class TrafficLights { |
| 10 | + |
| 11 | + public static void main(String[] args) throws IOException { |
| 12 | + |
| 13 | + // reader |
| 14 | + Scanner sc = new Scanner(System.in); |
| 15 | + |
| 16 | + // inputs |
| 17 | + int streetLength = sc.nextInt(); |
| 18 | + int numberOfTrafficLights = sc.nextInt(); |
| 19 | + sc.nextLine(); |
| 20 | + |
| 21 | + String[] positionOfTrafficLights = sc.nextLine().split("\\s+"); |
| 22 | + |
| 23 | + // writer |
| 24 | + BufferedOutputStream writer = new BufferedOutputStream(System.out); |
| 25 | + |
| 26 | + // solve |
| 27 | + NavigableSet<Integer> street = new TreeSet<>(List.of(0, streetLength)); |
| 28 | + TreeMap<Integer, Integer> passages = new TreeMap<>(); |
| 29 | + |
| 30 | + for (int i = 0; i < numberOfTrafficLights; i++) { |
| 31 | + Integer trafficLightPosition = Integer.parseInt(positionOfTrafficLights[i]); |
| 32 | + |
| 33 | + Integer nextLightPositon = street.higher(trafficLightPosition); |
| 34 | + Integer previousLightPosition = nextLightPositon != null ? street.lower(nextLightPositon) : null; |
| 35 | + |
| 36 | + if (previousLightPosition != null && nextLightPositon != null) { |
| 37 | + Integer passageToBeDivided = nextLightPositon - previousLightPosition; |
| 38 | + Integer leftDivision = trafficLightPosition - previousLightPosition; |
| 39 | + Integer rightDivision = nextLightPositon - trafficLightPosition; |
| 40 | + |
| 41 | + passages.put(passageToBeDivided, passages.getOrDefault(passageToBeDivided, 1) - 1); |
| 42 | + if (passages.getOrDefault(passageToBeDivided, 0) == 0) { |
| 43 | + passages.remove(passageToBeDivided); |
| 44 | + } |
| 45 | + |
| 46 | + passages.put(leftDivision, passages.getOrDefault(leftDivision, 0) + 1); |
| 47 | + passages.put(rightDivision, passages.getOrDefault(rightDivision, 0) + 1); |
| 48 | + } |
| 49 | + |
| 50 | + street.add(trafficLightPosition); |
| 51 | + |
| 52 | + writer.write((passages.lastKey() + " ").getBytes()); |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + writer.flush(); |
| 57 | + |
| 58 | + // close reader |
| 59 | + sc.close(); |
| 60 | + |
| 61 | + return; |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | +} |
0 commit comments