Test instructions: Give the number of n, each time you can move the first number to the last position, ask the nth permutation of the smallest inverse logarithm
First, we'll find out the logarithm of the first reverse order.
Then for a number a[i], a number smaller than it has a[i]-1, a number larger than it has n-a[i]
So the a[i] moved to the end of the sequence, the equivalent of the loss of a[i]-1 reverse order number, got n-a[i] the reverse number
For a total of n-2*a[i] + one
Do another n comparison to maintain a minimum value
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <Set>9#include <queue>Ten#include <algorithm> One using namespacestd; A -typedefLong LongLL; - Const intINF = (1<< -)-1; the Const intMod=1000000007; - Const intmaxn=50005; - - intN; + intA[MAXN],C[MAXN]; - + intLowbit (intx) {returnX & (-x);} A at intSumintx) { - intRET =0; - while(x>0){ -ret+=c[x];x-=lowbit (x); - } - returnret; in } - to voidAddintXintd) { + while(x<=N) { -c[x]+=d;x+=lowbit (x); the } * } $ Panax Notoginseng intMain () { - while(SCANF ("%d", &n)! =EOF) { theMemset (c,0,sizeof(c)); + for(intI=1; i<=n;i++) scanf ("%d", &a[i]), a[i]++; A the intans=0; + for(intI=1; i<=n;i++){ -Ans + = i-1-sum (a[i]); $ //printf ("ans=%d\n", ans); $Add (A[i],1); - } - intminn=INF; the for(intI=1; i<=n;i++){ -Ans + = n2*a[i] +1;Wuyiminn=min (minn,ans); the } -printf"%d\n", Minn); Wu } - return 0; About}
View Code
HDU 1394 Minimum Inversion number "tree-like array"