1

For the given problem statement we need to calculate the number of inversion in an array so i tried to apply an algorithm using merge sort and calculating the number of inversion while merging and also at time of sorting. While my code gives the same answer for the test cases i fed to the system as my own solutions i am getting a wrong answer on the online judge - Codechef. Please tell me my mistake.

problem link: http://www.codechef.com/COOK43/problems/LPAIR

code:

#include<iostream>
using namespace std;

long long int Merge(int* left,int* right,int* arr,int nl,int nr)
{
    int i=0;
    int j=0;
    int k=0;
    long long int cnt=0;
    while(i<nl&&j<nr)
    {
        if(left[i]<=right[j])
        {
            arr[k]=left[i];
            i++;
        }
        else
        {
            arr[k]=right[j];
            j++;
            cnt+=nl-i;
        }
        k++;
    }
    while(i<nl)
    {
        arr[k]=left[i];
        i++;
        k++;
    }
    while(j<nr)
    {
        arr[k]=right[j];
        j++;
        k++;
    }
    return cnt;
}

long long int MergeSort(int *a,int len)
{
    long long int cnt=0;
    if(len<2)
        return 0;
    int mid=len/2;
    int* left=new int[mid];
    int* right=new int[len-mid];
    for(int i=0;i<mid;i++)
        left[i]=a[i];
    for(int i=mid;i<len;i++)
        right[i-mid]=a[i];
    cnt+=MergeSort(left,mid);
    cnt+=MergeSort(right,len-mid);
    cnt+=Merge(left,right,a,mid,len-mid);
    delete(left);
    delete(right);
    return cnt;
}

int main()
{
    int n;
    cin>>n;
    int* fm=new int[n];
    for(int i=0;i<n;i++)
        cin>>fm[i]>>fm[i];
    cout<<MergeSort(fm,n);
}
InsaynAsasin
  • 47
  • 1
  • 2
  • 9
  • 2
    Formatting nitpicker... spaces, braces and comments are free, use them. ;-) – DevSolar Feb 21 '14 at 11:53
  • thats cheating..you know that..:P – Kamlesh Arya Feb 21 '14 at 11:55
  • Just curious, Were you planning on fixing the memory leaks in `MergeSort` before you submit this again? – WhozCraig Feb 21 '14 at 11:56
  • @WhozCraig I am just a newbie in programming field and was not able to find the problem in my code so i asked my problem here. But i didn't get whatever you said. – InsaynAsasin Feb 21 '14 at 12:02
  • It means your allocations your making for splitting into the left and right sides before recursing should be deleted *after* the merge is done, otherwise you're leaking memory. – WhozCraig Feb 21 '14 at 12:11
  • @WhozCraig i have updated the code and tried to prevent memory leakage by deleting the two temporary arrays. Can you tell me whether it is fine now or not and also what is the problem with my code? – InsaynAsasin Feb 21 '14 at 14:38
  • possible duplicate of [Counting inversions in an array](http://stackoverflow.com/questions/337664/counting-inversions-in-an-array) – Farm Oct 06 '14 at 23:40

2 Answers2

0

This is the correct approach and you have a very simple problem - the number of inversions for n = 105 may overflow integer. Think of how you can fix that.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • i changed the integer cnt to long long int and hence it is able to store the maximum possible inversions that is 10^5C2 = 4999950000 but i am still getting a wrong answer. – InsaynAsasin Feb 21 '14 at 12:09
  • i think yes. the number of n is the number of array elements which are at max 10^5 so an int array can store these much values but int cnt was not able to hold the number of inversions so i made it long long int. but it is still not working. – InsaynAsasin Feb 21 '14 at 12:14
  • i have updated the code. You can tell me now that what is the problem – InsaynAsasin Feb 21 '14 at 14:37
  • I wish I could. The only thing I notice is that you do not print a new line character after the answer. The code seems ligit to me. – Ivaylo Strandjev Feb 21 '14 at 15:53
0

The Error is input pairs of men and women will not given as sorted based on Male Chef Mi.

Before running merge sort you need to sort the pair based on Mi < Male number> because male should be standing in an increasing order.

so your array fm should be array of pairs and then sort this according to Mi.

And then all the operation in merge-sort will be based on second element of fm (fm.second)

Solution

Ritesh
  • 497
  • 4
  • 15