Пример #1
0
        static void Main(string[] args)
        {
            int t = Int32.Parse(Console.ReadLine());

            for (int i = 1; i < t + 1; i++)
            {
                string inputStr = Console.ReadLine();
                int    space    = inputStr.IndexOf(' ');
                long   n        = Int64.Parse(inputStr.Substring(0, space));
                long   k        = Int64.Parse(inputStr.Substring(space));

                List <Stall> bathroom = new List <Stall>(); //create bathroom (list of stalls) and install guards
                Stall        guardL   = new Stall(0);
                guardL.nearL = guardL;
                Stall guardR = new Stall(n + 1);
                guardR.nearR = guardR;
                guardL.makeNeighbors(guardR);
                bathroom.Add(guardL);
                bathroom.Add(guardR);

                for (int p = 0; p < k; p++) //occupy stalls
                {
                    long  greatestWidth = 0;
                    Stall leftBound     = new Stall(0); //instantiate as placeholders. their values will be replaced before the foreach loop ends
                    Stall rightBound    = new Stall(0);

                    foreach (var occupied in bathroom) //find the widest gap
                    {
                        if (occupied.lDist() > greatestWidth)
                        {
                            greatestWidth = occupied.lDist();
                            leftBound     = occupied.nearL;
                            rightBound    = occupied;
                        }
                        if (occupied.rDist() > greatestWidth)
                        {
                            greatestWidth = occupied.rDist();
                            leftBound     = occupied;
                            rightBound    = occupied.nearR;
                        }
                    }

                    Stall newOccupant = new Stall(leftBound.pos + (greatestWidth + 1) / 2);
                    newOccupant.makeNeighbors(leftBound);
                    newOccupant.makeNeighbors(rightBound);
                    bathroom.Add(newOccupant);

                    if (p == k - 1)
                    {
                        Console.WriteLine("Case #" + i + ": " + Math.Max(newOccupant.lDist(), newOccupant.rDist()) + " " + Math.Min(newOccupant.lDist(), newOccupant.rDist()));
                    }
                }
            }
        }
Пример #2
0
 public void makeNeighbors(Stall newNeighbor)
 {
     if (this.pos < newNeighbor.pos)
     {
         nearR             = newNeighbor;
         newNeighbor.nearL = this;
     }
     else
     {
         nearL             = newNeighbor;
         newNeighbor.nearR = this;
     }
 }
Пример #3
0
 public Stall(long p, Stall nL, Stall nR)
 {
     pos   = p;
     nearL = nL;
     nearR = nR;
 }