MiroslavKisov

TruckTour

Jan 22nd, 2018
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace TruckTour
  6. {
  7.     public class StartUp
  8.     {
  9.         public static void Main()
  10.         {
  11.             var fuelQueue = new Queue<long>();
  12.             var distanceQueue = new Queue<long>();
  13.             int index = 0;
  14.             int bestIndex = 0;
  15.             bool isPotentialIndexFound = true;
  16.             bool isIndexFound = false;
  17.             long currentFuel = 0L;
  18.             long numberOfPumps = long.Parse(Console.ReadLine());
  19.             for (int i = 0; i < numberOfPumps; i++)
  20.             {
  21.                 var currentPump = Console.ReadLine()
  22.                     .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  23.                     .Select(long.Parse)
  24.                     .ToArray();
  25.                 long fuel = currentPump[0];
  26.                 long distance = currentPump[1];
  27.  
  28.                 fuelQueue.Enqueue(fuel);
  29.                 distanceQueue.Enqueue(distance);
  30.             }
  31.             while (true)
  32.             {
  33.                 if (index >= fuelQueue.Count)
  34.                 {
  35.                     break;
  36.                 }
  37.                 isPotentialIndexFound = true;
  38.                 if (fuelQueue.Peek() < distanceQueue.Peek())
  39.                 {
  40.                     fuelQueue.Enqueue(fuelQueue.Dequeue());
  41.                     distanceQueue.Enqueue(distanceQueue.Dequeue());
  42.                     index++;
  43.                 }
  44.                 else
  45.                 {
  46.                     currentFuel = fuelQueue.Peek() - distanceQueue.Peek();
  47.                     for (int i = 0; i < distanceQueue.Count - 1; i++)
  48.                     {
  49.                         if (currentFuel < 0)
  50.                         {
  51.                             isIndexFound = false;
  52.                             currentFuel = 0;
  53.                             fuelQueue.Enqueue(fuelQueue.Dequeue());
  54.                             distanceQueue.Enqueue(distanceQueue.Dequeue());
  55.                             index++;
  56.                             break;
  57.                         }
  58.                         else
  59.                         {
  60.                             if (isPotentialIndexFound)
  61.                             {
  62.                                 bestIndex = index;
  63.                                 isPotentialIndexFound = false;
  64.                                 isIndexFound = true;
  65.                             }
  66.                             fuelQueue.Enqueue(fuelQueue.Dequeue());
  67.                             currentFuel += fuelQueue.Peek();
  68.                             distanceQueue.Enqueue(distanceQueue.Dequeue());
  69.                             currentFuel -= distanceQueue.Peek();
  70.                             index++;
  71.                         }
  72.                     }
  73.                 }
  74.             }
  75.             if (isIndexFound)
  76.             {
  77.                 Console.WriteLine(bestIndex);
  78.             }
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment