Guest User

Road Trip IEEE Extreme 2014

a guest
Nov 2nd, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1.  
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class Solution {
  7.     static int gasStations;
  8.     static Long maxFuel;
  9.     static Long startingFuel;
  10.     static Long length;
  11.  
  12.     private static class Station implements Comparable<Station> {
  13.         long  price;
  14.         long  pos;
  15.  
  16.         private Station(long  price, long pos) {
  17.             this.price = price;
  18.             this.pos = pos;
  19.         }
  20.  
  21.         @Override
  22.         public int compareTo(Station o) {
  23.             return Long.valueOf(price).compareTo(o.price);
  24.         }
  25.  
  26.         @Override
  27.         public String toString() {
  28.             return "{" + price + "$, " + pos + "}";
  29.         }
  30.     }
  31.  
  32.     public static void main(String[] args) {
  33.         Scanner scanner = new Scanner(System.in);
  34.         int M = Integer.parseInt(scanner.nextLine());
  35.         for (int i = 0; i < M; i++) {
  36.             String[] line = scanner.nextLine().split("\\s+");
  37.             gasStations = Integer.parseInt(line[0]);
  38.             maxFuel = Long.parseLong(line[1]);
  39.             startingFuel = Long.parseLong(line[2]);
  40.             length = Long.parseLong(line[3]);
  41.             Station[] stations = new Station[gasStations];
  42.             for (int s = 0; s < gasStations; s++) {
  43.                 line = scanner.nextLine().split("\\s+");
  44.                 stations[s] = new Station(Long.parseLong(line[1]), Long.parseLong(line[0]));
  45.             }
  46.             //SORTING STATIONS, CHEAPEST FIRST
  47.             Arrays.sort(stations);
  48.             checkSolution(stations);
  49.         }
  50.  
  51.     }
  52.  
  53.     private static void checkSolution(Station[] stations) {
  54.         long[] reserveFuel = {0};
  55.         System.out.println(recursiveCall(0, length, startingFuel, stations, reserveFuel));
  56.     }
  57.  
  58.     // RETURNS THE $$ SPENT IN FUEL
  59.     private static long recursiveCall(long start, long end, long fuel, Station[] stations, long [] reserveFuel) {
  60. //      System.out.println("RECURSIVING -- start:" + start + " end:" + end + " fuel:" + fuel);
  61.         // IF I HAVE ENOUGH FUEL TO GET TO THE END
  62.         if (end - start <= fuel) {
  63.             reserveFuel[0] = fuel - (end - start);
  64.             return 0; //SPENT NO CASH, I COULD GET THERE WITH THE CURRENT TANK
  65.         }
  66.  
  67.         Station station = null;
  68.         for (int s = 0; s < gasStations && station == null; s++) {
  69.             // GET THE FIRST (AND CHEAPEST) STATION BETWEEN MY CURRENT START AND END POSITIONS
  70.             if (stations[s] != null && stations[s].pos >= start && stations[s].pos < end) {
  71.                 station = stations[s];
  72.                 stations[s] = null; //NULLING OUT USED STATIONS
  73.             }
  74.         }
  75.         // THERE ARE NO STATIONS BETWEEN START AND END
  76.         if (station == null) {
  77.             return -1; // CAN'T GET THERE
  78.         }
  79.         long ans = 0; // TOTAL $ SPENT
  80.  
  81.         long var = recursiveCall(start, station.pos, fuel, stations, reserveFuel);
  82.         // IF I COULDN'T MAKE IT
  83.         if(var == -1)
  84.             return var;
  85.  
  86.         ans += var; // ADDING THE $$ NEEDED TO GET HERE
  87.  
  88.         long neededFuel = Math.min(maxFuel- reserveFuel[0], end - station.pos - reserveFuel[0]);
  89.  
  90.         long chargedFuel = reserveFuel[0] + neededFuel;
  91.         ans += (neededFuel * station.price); // ADDING WHAT I SPENT HERE
  92.        
  93. //      System.out.println("CHARGING FUEL -- $" + (neededFuel * station.price) + " in:" + station.pos);
  94.         reserveFuel[0] = 0;
  95.         var = recursiveCall(station.pos, end, chargedFuel, stations, reserveFuel);
  96.         // IF I COULDN'T MAKE IT
  97.         if(var == -1)
  98.             return var;
  99.  
  100.         ans += var; // ADDING WHAT I WILL SPEND IN THE REST OF THE TRIP TO MY GOAL        
  101.         return ans;
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment