Exemplo n.º 1
0
        static long solution(int n, int[] works)
        {
            long    answer = 0;
            int     size   = works.Length;
            maxHeap heap   = new maxHeap(size + 1);

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

            while (n != 0 && heap.Peek() != 0)
            {
                int max = heap.Delete();
                max--;
                n--;
                heap.Insert(max);
            }

            while (!heap.isEmpty())
            {
                int cur = heap.Delete();
                answer += (cur * cur);
            }

            return(answer);
        }
Exemplo n.º 2
0
        static void Main()
        {
            maxHeap       heap = new maxHeap(200000);
            StringBuilder sb   = new StringBuilder();
            int           n    = int.Parse(Console.ReadLine());

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

                if (read == 0)
                {
                    sb.AppendLine(heap.Delete().ToString());
                }
                else
                {
                    heap.Insert(read);
                }
            }

            Console.WriteLine(sb.ToString());
        }
Exemplo n.º 3
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());
        }