Advertisement
joro_thexfiles

Problem 7. Truck Tour - my variant

Jan 16th, 2020
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Stacks
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             long countOfPumps = long.Parse(Console.ReadLine());
  12.  
  13.             Queue<long> pumps = new Queue<long>();
  14.  
  15.             for (int i = 0; i < countOfPumps; i++)
  16.             {
  17.                 var data = Console.ReadLine()
  18.                     .Split()
  19.                     .Select(long.Parse)
  20.                     .ToArray();
  21.  
  22.                 long num1 = data[0];
  23.                 long num2 = data[1];
  24.                 pumps.Enqueue(num1);
  25.                 pumps.Enqueue(num2);
  26.             }
  27.  
  28.             long leftFuel = 0;
  29.             int index = 0;
  30.             int counter = 0;
  31.             int mainCounter = 0;
  32.  
  33.             while (true)
  34.             {
  35.                 var liters = pumps.Dequeue();
  36.                 var distance = pumps.Dequeue();
  37.  
  38.                 if (liters + leftFuel >= distance)
  39.                 {
  40.                     leftFuel = leftFuel + liters - distance;
  41.                     counter++;
  42.                 }
  43.                 else if (liters + leftFuel < distance)
  44.                 {
  45.                     index = mainCounter + 1;
  46.                     counter = 0;
  47.                 }
  48.  
  49.                 pumps.Enqueue(liters);
  50.                 pumps.Enqueue(distance);
  51.  
  52.                 if (counter >= countOfPumps)
  53.                 {
  54.                     Console.WriteLine(index);
  55.                     return;
  56.                 }
  57.                 mainCounter++;
  58.             }
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement