ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1126. Магнитные бури

Vitalii Arbuzov Why TL with n*log(m) solution? [2] // Задача 1126. Магнитные бури 6 мар 2011 18:43
Here is my solution. It uses heap(PriorityQueue) and queue(LinkedList). It TLs on 2nd test case. Is there any solution which is asymptotically better than this? What are points to improve?
public class P1126 {
    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        in.nextToken();
        int m = (int)in.nval;

        PriorityQueue<Integer> heap = new PriorityQueue<Integer>(m, new Comparator<Integer>() {
            public int compare(Integer i, Integer j) {
                return j.compareTo(i);
            }
        });
        LinkedList<Integer>  queue = new LinkedList<Integer>();
        int cur;
        while (true) {
            in.nextToken();
            cur = (int)in.nval;
            if (cur == -1) break;
            heap.add(cur);
            queue.add(cur);
            if (heap.size() == m) {
                System.out.println(heap.peek());
                heap.remove(queue.pollFirst());
            }
        }
    }
}
Alipov Vyacheslav [Tomsk PU] Re: Why TL with n*log(m) solution? // Задача 1126. Магнитные бури 7 мар 2011 15:27
Your solution's complexity is O(NM), because heap.remove(Object) requires linear time, not O(logN) time.
vlyubin Re: Why TL with n*log(m) solution? // Задача 1126. Магнитные бури 16 апр 2012 00:26
Just submit a brute-force. The easiest improvement is SQRT decomposition, but it's not needed in this case.