Guest User

Lamp.java

a guest
Dec 18th, 2016
77
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2.  
  3. public class Lamps {
  4.  
  5.     public static void main(String[] args) throws Exception {
  6.         Scanner scan = new Scanner(System.in);
  7.  
  8.         String s = scan.nextLine();
  9.         String[] sArray = s.split(" ");
  10.         int[] lampa = new int[sArray.length];
  11.  
  12.         for (int i = 0; i < sArray.length; i++) {
  13.             lampa[i] = Integer.valueOf(sArray[i]);
  14.         }
  15.  
  16.         System.out.println(solve(lampa));
  17.  
  18.     }
  19.  
  20.     public static int solve(int[] pole) {
  21.  
  22.         int x = 0;
  23.         int n = pole.length;
  24.         if (n > 5) {
  25.             int m = n / 2;
  26.  
  27.             int a = solve(Arrays.copyOfRange(pole, 0, (m)));
  28.             int b = solve(Arrays.copyOfRange(pole, m, (n)));
  29.             int c = solve(Arrays.copyOfRange(pole, 0, (m - 1)));
  30.             int d = solve(Arrays.copyOfRange(pole, (m + 1), (n)));
  31.  
  32.             x = Math.min((a + b), Math.min((c + b), (a + d)));
  33.         } else if (n == 5) {
  34.             x = Math.min(pole[2], (pole[1] + pole[3])) + pole[0] + pole[4];
  35.         } else if (n == 4) {
  36.             x = Math.min(pole[1], pole[2]) + pole[0] + pole[3];
  37.         } else if (n == 3) {
  38.             x = pole[0] + pole[pole.length - 1];
  39.         } else if (n == 2) {
  40.             x = pole[0] + pole[pole.length - 1];
  41.         } else if (n < 2) {
  42.             x = pole[0];
  43.         }
  44.  
  45.         return x;
  46.     }
  47.  
  48. }
RAW Paste Data