Frosh WeekTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2772 Accepted Submission (s): 923
Problem descriptionduring Frosh Week, students play various games-get to know all other and compete against other Teams. In one such-game, all the Frosh-a team stand in a line, and is then asked to arrange themselves according to some crit Erion, such as their height, their birth date, or their student number. This rearrangement of the line must is accomplished only by successively swapping pairs of consecutive students. The team that finishes fastest wins. Thus, in order to win, you would like to minimize the number of swaps required.
Inputthe first line of input contains one positive integer n, the number of students on the team, which'll be is no more th An one million. The following n lines each contain one integer and the student number of each student on the team. No student number would appear more than once.
Outputoutput a line containing the minimum number of swaps required to arrange the students in increasing order by student Number.
Sample Input
3312
Sample Output
2
The main idea: to seek the logarithm of reverse order number;
Thinking of solving problems: merging and sorting;
#include "iostream" #include "Cstdio" #include "CString" using namespace Std;int a[1000005];int b[1000005];//Auxiliary Array Long Long cnt;void Merge (int a[],int p,int q,int r, int b[]) {int s = p, t = q + 1, k = P;while (s <= q && t <= r) {if (A[s] <= a[t]) {b[k + +] = A[s + +];} Else{b[k + +] = a[t ++];cnt + = (q-s + 1);//In the original sequence A[S...Q] > a[t], there is an inverse number pair (q-s+1)}}if (s = = q + 1) for (; t <= r;t + +) B[k + +] = a[t];elsefor (; s <= q;s + +) B[k + +] = a[s];for (int i = P;i < K;i + +) a[i] = B[i];} void Bottomupsort (int a[],int first,int last,int b[]) {if (first < last) {int mid = (first + last)/2; Bottomupsort (A, first, Mid, b); Bottomupsort (A, mid + 1, last, b); Merge (A, first, mid, last, b);}} int main () {int n;while (scanf ("%d", &n)! = EOF) {cnt = 0;for (int i = 1;i <= n;i + +) scanf ("%d", &a[i]); Bottomupsort (a,1,n, b);p rintf ("%i64d\n", CNT);} return 0;}
Frosh Week (hdu_3743) Merge sort + reverse order number pair