Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com
And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Saturday, March 29, 2014

RMID - Running Median Revisited

Link to the question

Actually, its same RMID spoj question but Nishant Raj has already posted it. The Time taken by the code mentioned before is nearly 5 sec. But mine taking 0.14 sec . The only concept used over here is Doubly Linked List with few modification.

#include<stdio.h>
#include<stdlib.h>

struct node
{
    int val;
    struct node *left,*right;

}*start=NULL,*med=NULL,*temp;

int main()
{
    int i,j,k,n,count=0;

    while(scanf("%i",&n)!=EOF)
    {
        if(n>0)
        {
            ++count;

            temp = (struct node *)malloc(sizeof(temp));

            temp->val = n;
            temp->left = temp->right = NULL;

            if(med==NULL)
            {
                start = med = temp;
            }

            else
            {
                start->right = temp;
                temp->left = start;
                start = temp;

                if(count&1)
                {
                    med = med->right;
                }
            }
        }

        else if(n==-1)
        {
            temp = med;

            printf("%d\n",med->val);

            if(count&1)
                med = med->left;
            else
                med = med->right;

            count--;

            if(temp->left != NULL)
                temp->left->right = temp->right;
            if(temp->right != NULL)
                temp->right->left = temp->left;

            free(temp);
        }

        else
        {
            printf("\n");
            start = med = NULL;
            count = 0;
            fflush(stdin);
        }
    }

    return 0;
}