JulianJulianov

QueueC#

May 5th, 2021 (edited)
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.95 KB | None | 0 0
  1. 7.  Truck Tour
  2. Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N−1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump.
  3. Initially, you have a tank of infinite capacity carrying no petrol. You can start the tour at any of the petrol pumps. Calculate the first point from where the truck will be able to complete the circle. Consider that the truck will stop at each of the petrol pumps. The truck will move one kilometer for each liter of the petrol.
  4. Input
  5. • The first line will contain the value of N
  6. • The next N lines will contain a pair of integers each, i.e. the amount of petrol that petrol pump will give and the distance between that petrol pump and the next petrol pump
  7. Output
  8. • An integer which will be the smallest index of the petrol pump from which we can start the tour
  9. Constraints
  10. 1 ≤ N ≤ 1000001
  11. 1 ≤ Amount of petrol, Distance ≤ 1000000000
  12. Examples
  13. Input   Output
  14. 3       1
  15. 1 5
  16. 10 3
  17. 3 4    
  18.  
  19.  
  20.  
  21.  
  22. using System;
  23. using System.Collections.Generic;
  24.  
  25. namespace _7._Truck_Tour
  26. {
  27.     class Program
  28.     {
  29.         static void Main(string[] args)
  30.         {
  31.             var numPetrolPumps = int.Parse(Console.ReadLine());
  32.             var queue = new Queue<string[]>();
  33.             var counterTrueCircle = 0;
  34.             bool isCircle = false;
  35.             var sumPetrol = 0;
  36.             var sumKmToPomp = 0;
  37.             var index = 0;
  38.  
  39.             for (int i = 0; i < numPetrolPumps; i++)
  40.             {
  41.                 var infoForPetrol = Console.ReadLine().Split();
  42.                 queue.Enqueue((string.Join(' ', infoForPetrol) + ' ' + i.ToString()).Split());
  43.             }
  44.  
  45.             while (true)
  46.             {
  47.                 foreach (var item in queue)
  48.                 {
  49.                     if (int.Parse(item[0]) >= int.Parse(item[1]) || sumPetrol > sumKmToPomp)
  50.                     {
  51.                         sumPetrol += int.Parse(item[0]);
  52.                         sumKmToPomp += int.Parse(item[1]);
  53.                         if (sumPetrol >= sumKmToPomp)
  54.                         {
  55.                             counterTrueCircle++;
  56.                             if (counterTrueCircle == 1)
  57.                             {
  58.                                 index = int.Parse(item[2]);
  59.                             }
  60.  
  61.                             if (counterTrueCircle == numPetrolPumps)
  62.                             {
  63.                                 isCircle = true;
  64.                                 Console.WriteLine(index);
  65.                                 break;
  66.                             }
  67.                         }
  68.                         else
  69.                         {
  70.                             counterTrueCircle = 0;
  71.                             sumPetrol = 0;
  72.                             sumKmToPomp = 0;
  73.                             for (int i = 0; i < 1; i++)
  74.                             {
  75.                                 queue.Enqueue(queue.Peek());
  76.                                 queue.Dequeue();
  77.                             }
  78.                             break;
  79.                         }
  80.                     }
  81.                     else
  82.                     {
  83.                         counterTrueCircle = 0;
  84.                         sumPetrol = 0;
  85.                         sumKmToPomp = 0;
  86.                         for (int i = 0; i < 1; i++)
  87.                         {
  88.                             queue.Enqueue(queue.Peek());
  89.                             queue.Dequeue();
  90.                         }
  91.                         break;
  92.                     }
  93.                 }
  94.  
  95.                 if (isCircle == true)
  96.                 {
  97.                     break;
  98.                 }
  99.                 else
  100.                 {
  101.                     continue;
  102.                 }
  103.             }
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment