Esempio n. 1
0
        static int solution(int[] scoville, int K)
        {
            int answer = 0;

            minHeap heap = new minHeap(scoville.Length + 1);

            for (int i = 0; i < scoville.Length; i++)
            {
                heap.Insert(scoville[i]);
            }

            int first, second, newDish;

            while (heap.Peek() < K)
            {
                if (heap.heap_size == 1)
                {
                    return(-1);
                }

                first  = heap.Delete();
                second = heap.Delete();

                newDish = first + (second * 2);

                heap.Insert(newDish);
                answer++;
            }

            return(answer);
        }
Esempio n. 2
0
        static void Main()
        {
            maxHeap       maxheap = new maxHeap(200000);
            minHeap       minheap = new minHeap(200000);
            StringBuilder sb      = new StringBuilder();

            int n = int.Parse(Console.ReadLine());

            for (int i = 0; i < n; i++)
            {
                int data = int.Parse(Console.ReadLine());

                if (maxheap.heap_size == minheap.heap_size)
                {
                    maxheap.Insert(data);
                }
                else
                {
                    minheap.Insert(data);
                }

                if (!maxheap.isEmpty() && !minheap.isEmpty())
                {
                    if (maxheap.Peek() > minheap.Peek())
                    {
                        int temp = maxheap.Delete();
                        maxheap.Insert(minheap.Delete());
                        minheap.Insert(temp);
                    }
                }

                sb.AppendLine(maxheap.Peek().ToString());
            }

            Console.WriteLine(sb.ToString());
        }