Ejemplo n.º 1
0
        private bool _CollectDupSegments(
            int inDupCount,
            CSegmentPool inSegmentPool,
            int inMaxAllowedDup)
        {
            bool wasSuccessful = true;

            if (inDupCount > 0)
            {
                if (inDupCount <= inMaxAllowedDup)
                {
                    int theMinimalDistance = inSegmentPool.GetMinDist();
                    for (int i = 0, j = 0; i < _mArray.Length - 1 || j < inDupCount; i++)
                    {
                        var theDistance = Math.Abs(_mArray[i + 1]) - Math.Abs(_mArray[i]);
                        if (theDistance == theMinimalDistance && inSegmentPool.GetSegment(0).StartingPoint.Index != i
                            ) // Be careful about the same segment
                        {
                            int      theStartIndex = i, theEndIndex = i + 1;
                            CSegment theSegment = CSegment.Create(_mArray, theStartIndex, ref theEndIndex);
                            inSegmentPool.Add(theSegment);
                            j++;
                        }
                    }
                }
                else
                {
                    wasSuccessful = false;
                }
            }

            return(wasSuccessful);
        }
Ejemplo n.º 2
0
        public int Solve(int[] input)
        {
            // Implement your solution here, and return the correct result instead of 0

            int theResult = 0;

            if (input.Length < 5)
            {
                return(0);
            }

            Array.Sort(input);
            foreach (int i in input)
            {
                Console.Write(i + " ");
            }

            CPoint.SetBackingArray(input);
            CSegmentSquad theSegmentSquad = new CSegmentSquad(input);

            for (int i = 0; i < input.Length;)
            {
                bool canRecruit = theSegmentSquad.Recruit(ref i);
            }
            bool isFeasible = theSegmentSquad.FinalizeRecruit();

            if (!isFeasible)
            {
                return(0);
            }

            CAxe theAxe = new CAxe(theSegmentSquad);

            theSegmentSquad.Arm();

            CSegmentPool theMinimals    = theSegmentSquad.GetMinimals();
            CSegmentPool theSubMinimals = theSegmentSquad.GetSubMinimals();
            CSegmentPool thePool        = theMinimals;

            while (theAxe.CanAttack())
            {
                if (!theAxe.Attack(thePool))
                {
                    break;
                }

                if (thePool == theMinimals)
                {
                    if (theSubMinimals.GetMinDist() < thePool.GetMinDist())
                    {
                        thePool = theSubMinimals;
                    }
                }
                else
                {
                    if (theMinimals.GetMinDist() < theSubMinimals.GetMinDist())
                    {
                        thePool = theMinimals;
                    }
                }
            }

            if (theSegmentSquad.Disarmed())
            {
                CPoint[] theCage = theAxe.GetCage();
                foreach (var thePoint in theCage)
                {
                    theResult += Math.Abs(thePoint.Value);
                }
            }

            return(theResult);
        }