RMID - Running Median SPOJ Solution

 #include <iostream>
#include <list>
using namespace std;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    list<int> a;
    int tmp,count=1,n=0;
    list<int>::iterator it=a.begin();
    while(cin>>tmp){
        if(tmp>0){
            a.push_back(tmp);
            n++;
            if(n==1)it=a.begin();
        }
        else if(tmp==-1){
                while(count<(n+1)/2){
                    count++;
                    it++;
                }
                cout<<*it<<'\n';
                it=a.erase(it);
                n--;
                it--,count--;
        }
        else{
            a.clear();
            it=a.begin(),count=1,n=0;
            cout<<'\n';
        }
    }
}

Comments