Use merge sort to find the reverse order logarithm, only need to add cnt = n1-i in the proper place of the native merge sort OK. Well understood.
# Include <cstdio>
# Include <iostream>
Using namespace std;
Const int MAXM = 10003;
Const int INF = 0x7fffffff-1;
Int cnt;
// Merge algorithm in Merge Sorting
Void Merge (int array [], int start, int mid, int end)
{
Int temp1 [MAXM/2 + 1], temp2 [MAXM/2 + 1];
Int n1, n2;
N1 = mid-start + 1;
N2 = end-mid;
// Copy the First Half of the array
For (int I = 0; I <n1; I ++)
{
Temp1 [I] = array [start + I];
}
// Copy the half-end Array
For (int I = 0; I <n2; I ++)
{
Temp2 [I] = array [mid + I + 1];
}
// Set the following elements to a large value
Temp1 [n1] = temp2 [n2] = INF;
// Scan the two arrays one by one and place them in the corresponding positions.
For (int k = start, I = 0, j = 0; k <= end; k ++)
{
If (temp1 [I] <= temp2 [j])
{
Array [k] = temp1 [I];
I ++;
}
Else
{
Cnt + = n1-i; // Number of Reverse Order Pairs
Array [k] = temp2 [j];
J ++;
}
}
}
Www.2cto.com
// Merge and sort
Void MergeSort (int array [], int start, int end)
{
If (start <end)
{
Int I;
I = (end + start)/2;
// Sort the first half
MergeSort (array, start, I );
// Sort the second half
MergeSort (array, I + 1, end );
// Before and after the merge
Merge (array, start, I, end );
}
}
Int main ()
{
Int n, k;
Int arr [MAXM];
Int largemerge = 0;
Int num;
Scanf ("% d", & n, & k );
For (int j = 1; j <= k; j ++)
{
Cnt = 0;
For (int I = 0; I <n; I ++)
{
Scanf ("% d", arr + I );
}
MergeSort (arr, 0, n-1 );
If (cnt> largemerge)
{
Largemerge = cnt;
Num = j;
}
}
Printf ("% d \ n", num );
}