Sunday, July 24, 2016

UVA 11858 - Frosh Week

#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(0)
using namespace std;
int num[1048576], temp[1048576];
long long cnt;
void mergesort(int low , int high)
{
    if(low==high) return;
    int mid=(low+high)/2;
    mergesort(low,mid);
    mergesort(mid+1,high);
    int i , j , k;
    long long add=0;
    for(int i=low , j=mid+1 , k=low; k<=high; k++){
        if(i==mid+1)temp[k]=num[j++];
        else if(j==high+1){
                temp[k]=num[i++];
                cnt+=add;
        }
        else if(num[i]<=num[j]){
                temp[k]=num[i++];
                cnt+=add;
        }
        else{
            temp[k]=num[j++];
            add++;
        }
    }
    for(int k=low; k<=high; k++)num[k]=temp[k];
}
int main()
{
    FAST;
    long long n;
    while(cin>>n){
    cnt=0;
    for(int i=0; i<n; i++)cin>>num[i];
    mergesort(0,n-1);
   // for(int i=0; i<n; i++)cout<<num[i]<<" ";
    cout<<cnt<<'\n';
    }
    return 0;
}

No comments:

Post a Comment