Cod sursa(job #1489175)

Utilizator dan.ghitaDan Ghita dan.ghita Data 20 septembrie 2015 18:37:13
Problema Heapuri Scor 40
Compilator java Status done
Runda Arhiva educationala Marime 3.21 kb
import java.io.*;
import java.util.*;

public class Main {
    private static Scanner scanner;
    private static BufferedWriter bufferedWriter;
    private static List<Integer> heap = new ArrayList<>();
    private static List<Integer> insertedValues = new ArrayList<>();
    private static HashMap<Integer, Integer> deletedCount = new HashMap<>();

    public static void main(String args[]) throws IOException {
        heap.add(0);
        insertedValues.add(0);

        scanner = new Scanner(new FileInputStream("heapuri.in"));
        bufferedWriter = new BufferedWriter(new FileWriter("heapuri.out"));

        int type, n = scanner.nextInt();

        for (int i = 1; i <= n; ++i) {
            type = scanner.nextInt();
            switch (type) {
                case 1:
                    int value = scanner.nextInt();
                    insertedValues.add(value);
                    deletedCount.put(value, 0);
                    insert(heap, value);
                    break;
                case 2:
                    int removedIndex = scanner.nextInt();
                    int removedValue = insertedValues.get(removedIndex);
                    deletedCount.put(removedValue, deletedCount.get(removedValue) + 1);
                    break;
                case 3:
                    while (deletedCount.get(heap.get(1)) > 0) {
                        deletedCount.put(heap.get(1), deletedCount.get(heap.get(1)) - 1);
                        remove(heap, 1);
                    }
                    bufferedWriter.write(String.valueOf(heap.get(1)));
                    bufferedWriter.newLine();
            }
        }

        scanner.close();
        bufferedWriter.close();
    }

    public static void insert(List<Integer> heap, int value) {
        heap.add(value);
        percolate(heap, heap.size() - 1);
    }

    public static void remove(List<Integer> heap, int index) {
        heap.set(index, heap.get(heap.size() - 1));
        heap.remove(heap.size() - 1);

        if (index > 1 && heap.get(index) < heap.get(index >> 1))
            percolate(heap, index);
        else
            sift(heap, index);
    }

    public static void percolate(List<Integer> heap, int currentIndex) {
        while (currentIndex > 1) {
            int parentIndex = currentIndex >> 1;

            if (heap.get(currentIndex) < heap.get(parentIndex)) {
                Collections.swap(heap, currentIndex, parentIndex);
                currentIndex = parentIndex;
            } else {
                break;
            }
        }
    }

    public static void sift(List<Integer> heap, int currentIndex) {
        while (currentIndex << 1 < heap.size()) {
            int smallestChildIndex = currentIndex << 1;
            if (smallestChildIndex + 1 < heap.size() && heap.get(smallestChildIndex + 1) < heap.get(smallestChildIndex))
                smallestChildIndex++;

            if (heap.get(currentIndex) > heap.get(smallestChildIndex)) {
                Collections.swap(heap, currentIndex, smallestChildIndex);
                currentIndex = smallestChildIndex;
            } else {
                break;
            }
        }
    }
}