Example #1
0
        public static void Main()
        {
            int             n     = int.Parse(Console.ReadLine());
            Queue <GasPump> pumps = new Queue <GasPump>();

            for (int i = 0; i < n; i++)
            {
                int[] pumpInfo = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();

                int distanceToNext = pumpInfo[1];
                int amountOfGas    = pumpInfo[0];

                GasPump pump = new GasPump(distanceToNext, amountOfGas, i);

                pumps.Enqueue(pump);
            }

            GasPump starterPump     = null;
            bool    completeJourney = false;

            while (true)
            {
                GasPump currentPump = pumps.Dequeue();
                pumps.Enqueue(currentPump);

                starterPump = currentPump;
                int gasInTank = currentPump.amountOfGas;

                while (gasInTank >= currentPump.DistanceToNext)
                {
                    gasInTank -= currentPump.DistanceToNext;

                    currentPump = pumps.Dequeue();
                    pumps.Enqueue(currentPump);

                    if (currentPump == starterPump)
                    {
                        completeJourney = true;
                        break;
                    }
                    gasInTank += currentPump.amountOfGas;
                }
                if (completeJourney)
                {
                    Console.WriteLine(starterPump.indexOfPump);
                    break;
                }
            }
        }
Example #2
0
        static void Main()
        {
            int             n     = int.Parse(Console.ReadLine());
            Queue <GasPump> pumps = new Queue <GasPump>();

            for (int i = 0; i < n; i++)
            {
                string[] pumpInfo       = Console.ReadLine().Split();
                ulong    ammountOfGas   = ulong.Parse(pumpInfo[0]);
                ulong    distanceToNext = ulong.Parse(pumpInfo[1]);

                var pump = new GasPump(distanceToNext, ammountOfGas, i);
                pumps.Enqueue(pump);
            }

            GasPump starterPump     = null;
            bool    completeJourney = false;

            while (pumps.Count > 0)
            {
                GasPump currentPump = pumps.Dequeue();
                pumps.Enqueue(currentPump);
                starterPump = currentPump;

                ulong gastInTank = currentPump.AmountOfGas;
                while (gastInTank > currentPump.DistanceToNext)
                {
                    gastInTank -= currentPump.DistanceToNext;

                    currentPump = pumps.Dequeue();

                    pumps.Enqueue(currentPump);
                    if (currentPump == starterPump)
                    {
                        completeJourney = true;
                        break;
                    }
                    gastInTank += currentPump.AmountOfGas;
                }
                if (completeJourney)
                {
                    Console.WriteLine(starterPump.IndexOfPump);
                    break;
                }
            }
        }