MY name is ruhul amin

ISLAMIC UNIVERSITY OF KUSHTIA

Wednesday, April 3, 2013

uva solve problem 10810

#include<stdio.h>
#define max 10000000000
long s,L[500002],R[500002],a[500002];

void merge(long p,long q,long r)
{
    long n1,n2;
    long i,j,k;
    n1=q-p+1;
    n2=r-q;
    for(i=1;i<=n1;i++)
    L[i]=a[p+i-1];
    for(j=1;j<=n2;j++)
    R[j]=a[q+j];
    i=1;
    j=1;
    L[n1+1]=max;
    R[n2+1]=max;
    for(k=p;k<=r;k++)
    {
        if(L[i]<=R[j])
        {
            a[k]=L[i];
            i++;
        }
        else
        {
            a[k]=R[j];
            s+=n1-i+1;
            j++;
        }
    }

}

void ms(long p,long  r)
{
    long  q;
    if(p<r)
    {
        q=(p+r)/2;
        ms(p,q);
        ms(q+1,r);
        merge(p,q,r);
    }
}
int main()
{
long n;
long i;
while(scanf("%d",&n)!=EOF)
{
    if(n==0)
    break;
    for(i=1;i<=n;i++)
    scanf("%ld",&a[i]);
    i=1;
    s=0;
    ms(i,n);
    printf("%ld\n",s);

}
return 0;
}

No comments:

Post a Comment