Guest User

roblem6_TruckTour

a guest
May 17th, 2016
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Problem6_TruckTour {
  4.     public static void main(String[] args) {
  5.         Scanner input = new Scanner(System.in);
  6.  
  7.         int n = Integer.parseInt(input.nextLine());
  8.         Deque<PetrolStation> queue = new ArrayDeque<>();
  9.  
  10.         for (int i = 0; i < n; i++) {
  11.             String[] inputParams = input.nextLine().split(" ");
  12.             long petrolAmount = Long.parseLong(inputParams[0]);
  13.             long distanceToNextPump = Long.parseLong(inputParams[1]);
  14.             PetrolStation petrolPump = new PetrolStation(petrolAmount, distanceToNextPump);
  15.  
  16.             queue.addLast(petrolPump);
  17.         }
  18.  
  19.         int resultIndex = 0;
  20.         boolean solutionFound = false;
  21.         while (true) {
  22.             PetrolStation startStation = queue.removeFirst();
  23.             queue.addLast(startStation);
  24.  
  25.             long petrol = startStation.petrol;
  26.             petrol -= startStation.distanceToNextPump;
  27.             int currentStationsCount = 1;
  28.             while (petrol >= 0) {
  29.                 PetrolStation currentStation = queue.removeFirst();
  30.                 if (currentStation == startStation) {
  31.                     solutionFound = true;
  32.                     break;
  33.                 }
  34.  
  35.                 queue.addLast(currentStation);
  36.                 petrol += currentStation.petrol;
  37.                 petrol -= currentStation.distanceToNextPump;
  38.                 currentStationsCount++;
  39.             }
  40.  
  41.             if (solutionFound) {
  42.                 break;
  43.             }
  44.  
  45.             resultIndex += currentStationsCount;
  46.         }
  47.  
  48.         System.out.println(resultIndex);
  49.     }
  50. }
  51.  
  52. class PetrolStation {
  53.     public long petrol;
  54.     public long distanceToNextPump;
  55.  
  56.     public PetrolStation(long petrol, long distanceToNextStation) {
  57.         this.petrol = petrol;
  58.         this.distanceToNextPump = distanceToNextStation;
  59.     }
  60. }
Add Comment
Please, Sign In to add comment