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

WHY MLE ON TEST 7 PLEASE HELP MEEE
Posted by KiTe 26 Oct 2019 00:07
MY CODE IS:

#include<bits/stdc++.h>
using namespace std;

vector < int > heap;

/////MINHEAP

void ADD( int m )
{
  heap.push_back(m);
  int j = heap.size()-1;

  while( j != 1 &&  heap[j] < heap[j/2] )
  {
    //SWAP
    int carry = heap[j/2];
    heap[j/2] = heap[j];
    heap[j] = carry;

      j /= 2;
  }
  return;
}

void REMOVEMIN()
{
  heap[1] = heap[heap.size()-1];
  heap.pop_back();

  int j = 1;

  while( ( 2*j < heap.size() &&  heap[j] > heap[2*j] ) || ( 2*j+1 < heap.size() && heap[j] > heap[2*j+1] ) )
  {
      if( 2*j+1 < heap.size() && heap[2*j] > heap[2*j+1] )
      {
       //SWAP
       int carry = heap[j];
       heap[j] = heap[2*j+1];
       heap[2*j+1] = carry;
       j = 2*j+1;
      }
      else
      {
       //SWAP
       int carry = heap[j];
       heap[j] = heap[2*j];
       heap[2*j] = carry;
       j = 2*j;
      }
  }
  return;
}

int GETMIN()
{
  return heap[1];
}

int n, a;

int main()
{
  ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);

  heap.push_back(-1);

  cin >> n;
 if( n % 2 == 0 )
 {
     int m = n/2 + 1;
      while(n--)
      {
         cin >> a;
         ADD(a);

        if( heap.size() - 1 > m )
        {
          REMOVEMIN();
        }
      }
  int mid1 = GETMIN();
  REMOVEMIN();
  int mid2 = GETMIN();
  bool f = 0;

  if( mid1 % 2 == 0 && mid2 % 2 == 0 )
    cout << mid1/2 + mid2/2;
  else if( ( mid1 % 2 == 1 && mid2 % 2 == 0 ) || ( mid1 % 2 == 0  && mid2 % 2 == 1 ) )
    cout << mid1/2 + mid2/2 << ".5";
  else
    cout << mid1/2 + mid2/2 + 1;
 }
 else
 {
     int m = n/2 + 1;
      while(n--)
      {
         cin >> a;
         ADD(a);
         if( heap.size() - 1 > m )
        {
          REMOVEMIN();
        }
      }
     cout << GETMIN();
 }


  return 0;
}
BUT WHY MLE? ?:'(

Edited by author 26.10.2019 00:08

Edited by author 26.10.2019 00:09

Edited by author 26.10.2019 00:09

Edited by author 26.10.2019 00:09