Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _06.TruckTour
- {
- class Program
- {
- static void Main(string[] args)
- {
- var n = int.Parse(Console.ReadLine());
- var queue = new Queue<GasPump>();
- int truckFuel = 0;
- for (int i = 0; i < n; i++)
- {
- var input = Console.ReadLine()
- .Split()
- .Select(int.Parse)
- .ToArray();
- var currentPump = new GasPump()
- {
- Index = i,
- Capacity = input[0],
- DistanceToNext = input[1]
- };
- queue.Enqueue(currentPump);
- }
- //----------------------
- bool isFound = false;
- while (!isFound)
- {
- var current = queue.Dequeue();
- var firstPump = truckFuel + current.Capacity;
- if (firstPump - current.DistanceToNext >= 0)
- {
- firstPump -= current.DistanceToNext;
- var copyQueue = new Queue<GasPump>(queue);
- int count = 0;
- while (true)
- {
- var nextPupm = copyQueue.Dequeue();
- firstPump += nextPupm.Capacity;
- if (firstPump - nextPupm.DistanceToNext >= 0)
- {
- firstPump -= nextPupm.DistanceToNext;
- copyQueue.Enqueue(nextPupm);
- count++;
- if (count == copyQueue.Count)
- {
- isFound = true;
- break;
- }
- }
- else
- {
- queue.Enqueue(current);
- break;
- }
- }
- }
- else
- {
- queue.Enqueue(current);
- }
- if (isFound)
- {
- Console.WriteLine(current.Index);
- break;
- }
- }
- }
- }
- class GasPump
- {
- public int Index { get; set; }
- public int Capacity { get; set; }
- public int DistanceToNext { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement