Test instructions is very simple, see the case can understand, a sequence of length n, to reorder, ascending, each time take this number and the following all the number of comparisons, need to move as far back as possible to move, asked to operate several times
Analysis of the case, from the back to the front, if found earlier than the current number of the number of answers to add 1, but found that it is difficult to discuss clearly, but also has always been analyzed, the current number, if the number is smaller than it, it must be operated once, it is thought that the tree array in reverse order number of an operation, The tree array to reverse the number is the need to find out the current tree array of how many number than the current number is small, so thought, the array of numbers from the back to the previous one into the tree array, each plus a, to determine whether the tree array is smaller than the current number of numbers, there is an answer to add 1, One of the reasons for this is that the title emphasizes that the numbers in this sequence must be different numbers from 1 to N.
int T;int n;int nnum[1000000 + 55];int c[1000000 + 55];int case = 0;void init () {memset (nnum,0,sizeof (Nnum)); Memset (C,0,si Zeof (c));} bool Input () {while (cin>>n) {for (int i=1;i<=n;i++) scanf ("%d", &nnum[i]); return false;} return true;} int lowbit (int x) {return x& (-X);} void Add (int i,int val) {while (I <= N) {c[i] + = Val;i + lowbit (i);}} int get_sum (int i) {int sum = 0;while (i > 0) {sum + = c[i];i-= Lowbit (i);} return sum;} void Cal () {int ans = 0;for (int i=n;i>0;i--) {Add (nnum[i],1); int now = Get_sum (Nnum[i] – 1); if (now > 0) ans++;} printf ("Case #%d:%d\n", ++case,ans);} void output () {}int main () {cin>>t;while (t--) {init (); if (input ()) return 0;cal (); output ();} return 0;}
HDU5122 K.bro sorting tree Array class inverse number application