Advertisement
vladimirVenkov

Coki Skoki

Jul 30th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.Arrays;
  6. import java.util.HashSet;
  7. import java.util.Objects;
  8. import java.util.Scanner;
  9. import java.util.stream.Collectors;
  10. import java.util.stream.Stream;
  11.  
  12. public class CokiSkoki {
  13.     static void fakeInput() {
  14.         String input = "6\n" +
  15.                 "1 4 2 6 3 4\n";
  16.         System.setIn(new ByteArrayInputStream(input.getBytes()));
  17.     }
  18.  
  19.     public static void main(String[] args) throws IOException {
  20.         //fakeInput();
  21.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  22.  
  23.         int numberOfBuildings = Integer.parseInt(br.readLine());
  24.         String[] strNums = br.readLine().split(" ");
  25.  
  26.         int[] buildings = new int[numberOfBuildings];
  27.         int[] nextBuildingIndex = new int[numberOfBuildings];
  28.         int[] totalJumps = new int[numberOfBuildings];
  29.         for (int i = 0; i < buildings.length; i++) {
  30.             buildings[i] = Integer.parseInt(strNums[i]);
  31.         }
  32.  
  33.         nextBuildingIndex[buildings.length - 1] = 0;
  34.         totalJumps[buildings.length - 1] = 0;
  35.  
  36.         for (int i = buildings.length - 2; i > -1; i--) {
  37.  
  38.             for (int j = i + 1; j < buildings.length; j++) {
  39.                 if (buildings[j] > buildings[i]) {
  40.                     nextBuildingIndex[i] = j;
  41.                     break;
  42.                 }
  43.             }
  44.  
  45.             if (nextBuildingIndex[i] > 0) {
  46.                 totalJumps[i] += totalJumps[nextBuildingIndex[i]] + 1;
  47.             }
  48.         }
  49.         System.out.println(Arrays.stream(totalJumps).max().getAsInt());
  50.         System.out.println(Arrays.stream(totalJumps).boxed().map(Objects::toString).collect(Collectors.joining(" ")));
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement