Beispiel #1
0
        //[SolutionMethod]
        public Int64 Straight(FairCutSample sample)
        {
            Int64[] arr = sample.arr;
            Int64   k   = sample.k;



            return(FCut(k, new List <Int64>(), arr.ToList()));
        }
Beispiel #2
0
        public TAnswer DP(TSample Sample)
        {
            FairCutSample sample = Sample as FairCutSample;

            Int64[] arr = sample.arr;
            Int64   k   = sample.k;


            /*
             * if (k > (arr.Length / 2))
             * {
             *  k = arr.Length - k;
             * }
             */

            Int64 n = arr.Length;

            Array.Sort(arr);

            Int64[,] dp = new Int64[arr.Length + 1, arr.Length + 1];

            for (int i = 0; i < arr.Length + 1; i++)
            {
                for (int j = 0; j < arr.Length + 1; j++)
                {
                    dp[i, j] = Int64.MaxValue;
                }
            }

            dp[0, 0] = 0;


            for (int i = 0; i < arr.Length; i++)
            {
                for (int j = 0; j < i + 1; j++)
                {
                    if ((j > k) || ((i - j) > (n - k)))
                    {
                        continue;
                    }



                    Int64 temp_li = dp[i, j] + arr[i] * (i - j - (n - k - (i - j)));

                    Int64 temp_lu = dp[i, j] + arr[i] * (j - (k - j));

                    if (dp[i + 1, j + 1] > temp_li)
                    {
                        dp[i + 1, j + 1] = temp_li;
                    }
                    if (dp[i + 1, j] > temp_lu)
                    {
                        dp[i + 1, j] = temp_lu;
                    }
                }
            }



            return(new FairCutAnswer()
            {
                result = dp[n, k]
            });
        }
Beispiel #3
0
        //[SolutionMethod]
        public Int64 Greedy(FairCutSample sample)
        {
            Int64[] arr = sample.arr;
            Int64   k   = sample.k;

            if (k > (arr.Length / 2))
            {
                k = arr.Length - k;
            }


            Array.Sort(arr);


            Int64[,] Diffs = new Int64[arr.Length, arr.Length];

            for (int i = 0; i < arr.Length; i++)
            {
                for (int j = 0; j < arr.Length; j++)
                {
                    Diffs[i, j] = Math.Abs(arr[i] - arr[j]);
                }
            }

            /*
             * Int64 MassCenter = arr.Length / 2;
             *
             * List<Int64> Taken = new List<int>() { MassCenter };
             * List<Int64> Remains = new List<int>();
             * bool[] takes = new bool[arr.Length];
             * takes[MassCenter] = true;
             * Int64 distance = -2;
             * for (int i = 0; i < k; i++)
             * {
             *  Int64 toTake = Math.Min(MassCenter + distance, arr.Length-1);
             *  Taken.Add(toTake);
             *  takes[toTake] = true;
             *  if (distance > 0)
             *  {
             *      distance *= -1;
             *
             *  }
             *  else {
             *      distance *= -1;
             *      distance += 2;
             *
             *  }
             *  if (Taken.Count == k)
             *  {
             *      break;
             *  }
             *
             * }
             * for (int i = 0; i < arr.Length; i++)
             * {
             *  if (!takes[i])
             *  {
             *      Remains.Add(i);
             *  }
             * }
             *
             *
             * Int64 d = SetDiffs(Taken, Remains, Diffs);
             * return d;*/

            Boolean[] takes = new Boolean[arr.Length];
            for (int i = 0; i < arr.Length; i++)
            {
                takes[i] = false;
            }

            Int64 len01 = k * 2;

            Int64   Start01 = (arr.Length - len01) / 2;
            Boolean take    = false;

            for (int i = 0; i < k * 2; i++)
            {
                takes[i + Start01] = take;
                take = !take;
            }

            List <Int64> Taken   = new List <Int64>();
            List <Int64> Remains = new List <Int64>();

            for (int i = 0; i < arr.Length; i++)
            {
                if (takes[i])
                {
                    Taken.Add(i);
                }
                else
                {
                    Remains.Add(i);
                }
            }
            Int64 d = SetDiffs(Taken, Remains, Diffs);

            return(d);
        }
Beispiel #4
0
        //[SolutionMethod]
        public FairCutAnswer Solution1(TSample Sample)
        {
            FairCutSample sample = Sample as FairCutSample;

            Int64[] arr = sample.arr;
            Int64   k   = sample.k;

            Array.Sort(arr, new Comparison <Int64>((x, y) => y.CompareTo(x)));

            Int64[,] Diffs = new Int64[arr.Length, arr.Length];
            for (int i = 0; i < arr.Length; i++)
            {
                for (int j = 0; j < arr.Length; j++)
                {
                    Diffs[i, j] = Math.Abs(arr[i] - arr[j]);
                }
            }



            Int64[] Taken   = new Int64[k];
            Int64[] Remains = new Int64[arr.Length - k];

            for (int i = 0; i < k; i++)
            {
                Taken[i] = i;
            }

            for (int i = 0; i < arr.Length - k; i++)
            {
                Remains[i] = i + k;
            }

            Int64 CurrentDiff = SetDiffs(Taken, Remains, Diffs);


            for (int i = 0; i < k; i++)
            {
                for (int j = 0; j < arr.Length - k; j++)
                {
                    Int64 t = Taken[i];
                    Taken[i]   = Remains[j];
                    Remains[j] = t;

                    Int64 NewDiff = SetDiffs(Taken, Remains, Diffs);
                    if (NewDiff >= CurrentDiff)
                    {
                        Remains[j] = t;
                        Remains[j] = Taken[i];
                        Taken[i]   = t;
                    }
                    else
                    {
                        CurrentDiff = NewDiff;
                    }
                }
            }


            return(new FairCutAnswer()
            {
                result = CurrentDiff
            });
        }