Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=5775
Main Topic :
The rules for bubbling sorting are, at first, a permutation of a given 1~n, to find the difference in the farthest position that each number appears in the sort process.
for (int i=1;i<=n;++i) for (int j=n,t;j>i;-j) if (p[j-1] > P[j]) t=p[j],p[j]=p[j-1],p[j-1]=t;
Topic Ideas:
"Merge Sort" "Inverse number"
First, the number of left shifts and the number of right shifts is determined (the number on the left is larger than it and the number smaller on the right)
According to the rules, each time is to find the small number I exchange to position I, so a number will only go to the left once, does not exist first move to the left and then to the right to move to the left.
So the farthest position of a number is the maximum of these two numbers.
Then you can find a number of the number of numbers on the right of a count by merging the numbers of AI, by calculating the number of the left is larger than it, and then take Max.
1 //2 //by Coolxxx3 //4#include <iostream>5#include <algorithm>6#include <string>7#include <iomanip>8#include <memory.h>9#include <time.h>Ten#include <stdio.h> One#include <stdlib.h> A#include <string.h> - //#include <stdbool.h> -#include <math.h> the #defineMin (a) < (b) ( A):(B)) - #defineMax (a) (a) > (b)? ( A):(B)) - #defineABS (a) ((a) >0? ( A):(-(a))) - #defineLowbit (a) (a& (a)) + #defineSqr (a) ((a) * (a)) - #defineSwap (a) (a) ^= (b), (b) ^= (a), (a) ^= (b)) + #defineEPS (1E-8) A #defineJ 10000000 at #defineMAX 0x7f7f7f7f - #definePI 3.1415926535897 - #defineN 100004 - using namespacestd; -typedefLong LongLL; - intCas,cass; in intN,m,lll,ans; - intP[n],a[n],b[n],le[n],ri[n],num[n]; to voidMergeintS[],intLintMidintR) + { - inti,j,k,n1=mid-l+1, n2=r-mid; the for(i=1; i<=n1;i++) le[i]=s[l+i-1]; * for(i=1; i<=n2;i++) ri[i]=s[mid+i]; $le[n1+1]=ri[n2+1]=MAX;Panax Notoginseng for(i=j=1, k=l;k<=r;k++) - { the if(le[i]<=Ri[j]) +s[k]=le[i++]; A Else thes[k]=ri[j++],b[s[k]]+=n1-i+1; + } - } $ voidMergeSort (intS[],intLintR) $ { - intMid= (l+r) >>1; - if(l<R) the { - mergesort (s,l,mid);WuyiMergeSort (s,mid+1, R); the merge (s,l,mid,r); - } Wu } - intMain () About { $ #ifndef Online_judge -Freopen ("1.txt","R", stdin); - //freopen ("2.txt", "w", stdout); - #endif A inti,j; + //for (scanf ("%d", &cas); cas;cas--) the for(SCANF ("%d", &cas), cass=1; cass<=cas;cass++) - //while (~scanf ("%s", s)) $ //while (~scanf ("%d", &n)) the { thememset (b,0,sizeof(num)); theprintf"Case #%d:", Cass); thescanf"%d",&n); - for(i=1; i<=n;i++) in { thescanf"%d",&num[i]); thep[num[i]]=i; About } theMergeSort (NUM,1, n); the for(i=1; i<=n;i++) thea[i]=b[i]+i-P[i]; + for(i=1; i<=n;i++) -printf"%d%c", Max (A[i],b[i]), i==n?'\ n':' '); the }Bayi return 0; the } the /* - // - the // the */
View Code
"Merge Sort" "Inverse number" HDU 5775 Bubble sort