[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Define maxn1000100
Int input [MAXN] = {0 };
Int tmp [MAXN];
Void merge (int left, int middle, int right)
{
Int I, j, k;
I = left, j = middle + 1, k = 1;
While (I <= middle & j <= right)
{
If (input [j] <input [I])
{
Tmp [k ++] = input [j ++];
}
Else
{
Tmp [k ++] = input [I ++];
}
}
While (I <= middle)
Tmp [k ++] = input [I ++];
While (j <= right)
Tmp [k ++] = input [j ++];
For (I = left, k = 1; I <= right; I ++, k ++)
Input [I] = tmp [k];
}
Void merge_sort (int left, int right)
{
If (left <right)
{
Int middle = (left + right)/2;
Merge_sort (left, middle );
Merge_sort (middle + 1, right );
Merge (left, middle, right );
}
}
Int main (){
Int n, I;
While (scanf ("% d", & n)
{
For (I = 0; I <n; ++ I)
Scanf ("% d", & input [I]);
Merge_sort (0, n-1 );
For (I = 0; I <n-1; ++ I)
Printf ("% d", input [I]);
Printf ("% d \ n", input [I]);
}
Return 0;
}
The above is the Template
You only need to read the code to understand how it is implemented.
The following is an application XMU1328
1328. Unhappy bobo
Time Limit: 2000 MS Memory Limit: 65536 K
Total Submissions: 589 (85 users) Accepted: 55 (42 users)
Description
Bobo has n younger siblings in height: a1, a2 ...... An, bobo looked at them as tall and low, so they were very unhappy. It was defined that when two younger siblings did not follow the height of the station, bobo's unhappy value plus 1 (I, j ), I <j, ai> aj.
Input
An integer of n indicates that bobo has n younger siblings.
Here are n Integers of ai, representing the height of the younger brother.
N <= 10 ^ 6, ai <2 ^ 32
Output
An integer is the unhappy value of bobo.
Sample Input
5
5 4 3 2 1
Sample Output
10
# Include <stdio. h>
# Include <stdlib. h>
# Define maxn1000100
Int input [MAXN] = {0 };
Int tmp [MAXN];
Long result;
Void merge (int left, int middle, int right)
{
Int I, j, k;
I = left, j = middle + 1, k = 1;
While (I <= middle & j <= right)
{
If (input [j] <input [I])
{
Tmp [k ++] = input [j ++];
Result + = middle-I + 1;
}
Else
{
Tmp [k ++] = input [I ++];
}
}
While (I <= middle)
Tmp [k ++] = input [I ++];
While (j <= right)
Tmp [k ++] = input [j ++];
For (I = left, k = 1; I <= right; I ++, k ++)
Input [I] = tmp [k];
}
Void merge_sort (int left, int right)
{
If (left <right)
{
Int middle = (left + right)/2;
Merge_sort (left, middle );
Merge_sort (middle + 1, right );
Merge (left, middle, right );
}
}
Int main (){
Int n, I;
Scanf ("% d", & n );
For (I = 0; I <n; ++ I)
Scanf ("% d", & input [I]);
Result = 0;
Merge_sort (0, n-1 );
Printf ("% lld \ n", result );
Return 0;
}
The question below is the same as the question above.
Frosh Week
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 1027 Accepted Submission (s): 319
Problem Description
During Frosh Week, students play various fun games to get to know each other and compete against other teams. in one such game, all the frosh on a team stand in a line, and are then asked to arrange themselves according to some criterion, such as their height, their birth date, or their student number. this rearrangement of the line must be accomplished only by successively swapping pairs of consecutive students. the team that finishes fastest wins. thus, in order to win, you wowould like to minimize the number of swaps required.
Input
The first line of input contains one positive integer n, the number of students on the team, which will be no more than one million. the following n lines each contain one integer, the student number of each student on the team. no student number will appear more than once.
Output
Output a line containing the minimum number of swaps required to arrange the students in increasing order by student number.
Sample Input
3
3
1
2
Sample Output
2
Source
[Html]
# Include <stdio. h>
# Include <malloc. h>
Int ans; // Accuracy
_ Int64 a [1000000 + 50];
Void merge (int left, int mid, int right)
{
Int I, j, cnt = 0;
Int * p;
P = (int *) malloc (right-left + 1) * sizeof (int ));
I = left;
J = mid + 1;
While (I <= mid & j <= right) // at this time, the Parts pointed to by I and j are sorted and merged.
{
If (a [I] <= a [j])
{
P [cnt ++] = a [I];
I ++;
}
Else
{
P [cnt ++] = a [j];
J ++;
Ans + = mid-I + 1; // the I-th is larger than that of j. Since I has been sorted from small to large, I + 1 to mid will be larger than j.
}
}
While (I <= mid)
{
P [cnt ++] = a [I ++];
}
While (j <= right)
{
P [cnt ++] = a [j ++];
}
Cnt = 0;
For (I = left; I <= right; I ++)
A [I] = p [cnt ++];
Free (p );
}
Void merge_sort (int left, int right)
{
If (left <right) // The length is greater than 1. This judgment is not a loop.
{
Int mid;
Mid = (left + right)/2;
Merge_sort (left, mid );
Merge_sort (mid + 1, right );
Merge (left, mid, right );
}
}
Int main ()
{
Int n, I;
While (scanf ("% d", & n )! = EOF)
{
Ans = 0;
For (I = 0; I <n; I ++)
Scanf ("% d", & a [I]);
Merge_sort (0, n-1 );
Printf ("% I64d \ n", ans );//////////
}
Return 0;
}