Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _06.TruckTour
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             var n = int.Parse(Console.ReadLine());
  12.             var queue = new Queue<GasPump>();
  13.  
  14.             int truckFuel = 0;
  15.             for (int i = 0; i < n; i++)
  16.             {
  17.                 var input = Console.ReadLine()
  18.                     .Split()
  19.                     .Select(int.Parse)
  20.                     .ToArray();
  21.  
  22.                 var currentPump = new GasPump()
  23.                 {
  24.                     Index = i,
  25.                     Capacity = input[0],
  26.                     DistanceToNext = input[1]
  27.                 };
  28.                 queue.Enqueue(currentPump);
  29.             }
  30.  
  31.             //----------------------
  32.             bool isFound = false;
  33.             while (!isFound)
  34.             {
  35.                 var current = queue.Dequeue();
  36.                 var firstPump = truckFuel + current.Capacity;
  37.                 if (firstPump - current.DistanceToNext >= 0)
  38.                 {
  39.                     firstPump -= current.DistanceToNext;
  40.                     var copyQueue = new Queue<GasPump>(queue);
  41.                     int count = 0;
  42.                     while (true)
  43.                     {
  44.                         var nextPupm = copyQueue.Dequeue();
  45.                         firstPump += nextPupm.Capacity;
  46.                         if (firstPump - nextPupm.DistanceToNext >= 0)
  47.                         {
  48.                             firstPump -= nextPupm.DistanceToNext;
  49.  
  50.                             copyQueue.Enqueue(nextPupm);
  51.                             count++;
  52.                             if (count == copyQueue.Count)
  53.                             {
  54.                                 isFound = true;
  55.                                 break;
  56.                             }
  57.                         }
  58.                         else
  59.                         {
  60.                             queue.Enqueue(current);
  61.                             break;
  62.                         }
  63.                     }
  64.                 }
  65.                 else
  66.                 {
  67.                     queue.Enqueue(current);
  68.                 }
  69.                 if (isFound)
  70.                 {
  71.                     Console.WriteLine(current.Index);
  72.                     break;
  73.                 }
  74.             }
  75.         }
  76.     }
  77.  
  78.     class GasPump
  79.     {
  80.         public int Index { get; set; }
  81.         public int Capacity { get; set; }
  82.         public int DistanceToNext { get; set; }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement