传奇之MLE

P1923 【深基9.例4】求第 k 小的数

Arabidopsis @ 2024-11-09 09:50:52

如下,这段代码导致后两个点直接MLE,有没有好人看看为什么(

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Issues.QuestionList2
{
    internal class FindMinimumK
    {
        internal static void Run(string[] args)
        {
            var dataSize = Console.ReadLine().Split(' ');

            var n = Convert.ToInt32(dataSize[0]);
            var index = Convert.ToInt32(dataSize[1]);

            var data = Console.ReadLine().Split(' ');
            int[] array = new int[n];

            for (int i = 0; i < n; i++)
            {
                array[i] = Convert.ToInt32(data[i]);
            }

            var km = GetLast(array, 0, array.Length - 1, index);
            Console.WriteLine(km.ToString());
        }

        static int GetLast(int[] array,int low ,int high ,int dest)
        {
            var pivot = Partition(array, low, high);

            if (pivot == dest)
                return array[pivot];
            else if (pivot < dest)
                return GetLast(array, pivot+1, high, dest);
            else
                return GetLast(array, low, pivot-1, dest);
        }

        static int Partition(int[] array, int low, int high)
        {
            var pivot = array[1];

            var left = low;
            var right = high;

            var direction = 1;
            while (left != right)
            {
                if (direction == 1)
                {
                    if (array[right] < pivot)
                    {
                        array[left] = array[right];
                        direction = 0;
                        left++;
                    }
                    else
                        right--;
                }
                else
                {
                    if (array[left] > pivot)
                    {
                        array[right] = array[left];
                        direction = 1;
                        right--;
                    }
                    else
                        left++;
                }
            }
            array[left] = pivot;
            return left;
        }

    }
}

|