ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1306. Sequence Median

I've tried everything but still MLE
Posted by achvanov 30 Mar 2019 12:36
I've tried priority_queue and heap, but still MLE. Here's my code, can somebody help me?
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
    int n, a;
    std::cin >> n;
    std::vector<int> v(n / 2 + 1);
    for (int i = 0; i < n / 2 + 1; ++i) {
        std::cin >> v[i];
    }
    std::make_heap(v.begin(), v.end());
    for (int i = 0; i < (n + 1) / 2 - 1; ++i) {
        std::cin >> a;
        v.push_back(a);
        std::push_heap(v.begin(), v.end());
        std::pop_heap(v.begin(), v.end());
        v.pop_back();
    }
    std::sort_heap(v.begin(), v.end());
    if (n % 2) {
        std::cout << v.back() << ".0";
    }
    else {
        a = v.back();
        v.pop_back();
        std::cout << (0ll + a + v.back()) / 2 << '.';
        if ((0ll + a + v.back()) % 2) {
            std::cout << 5;
        }
        else {
            std::cout << 0;
        }
    }

    return 0;
}
It's WA.
Posted by lnxdx 9 Sep 2019 22:16
It's wrong. Because the last leaf of the max heap is not necessarily minimum but you are removing it (v.pop_back();). Your code may remove the median element.

And the sort_heap has no effect.