Ural 1090. In the Army Now problem solving report

Source: Internet
Author: User

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 );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.