Test instructions: There is a sequence of length n, the number of the sequence is a 0~n-1 composition, and then this sequence can be considered as a ring, then there is n a sequence of n length, ask the number of the smallest in the n sequence.
First, the initial sequence of the reverse number of the calculation, and then move each start number A to the back, the reverse number of changes is larger than a number of numbers plus one, less than a small number of reverse order minus one, so according to this rule again cycle to find the minimum value can be.
#include <cstdio>#include <cstring>#include <algorithm>using namespace STD;Const intN =5005;intN, C[n], a[n];intLowbit (intx) {returnX & (-X);}intSum (intx) {intRET =0; while(X >0) {ret + = c[x]; X-= Lowbit (x); }returnRET;}voidADD (intXintD) { while(x <= N) {C[x] + = D; x + = Lowbit (x); }}intMain () { while(scanf("%d", &n) = =1) {memsetC0,sizeof(C));intsum =0; for(inti =1; I <= N; i++) {scanf("%d", &a[i]); ADD (A[i] +1,1); Sum + = I-sum (A[i] +1); }intres = sum; for(inti =1; I < n; i++) {sum = sum + (n-1-A[i])-a[i]); res = min (res, sum); }printf("%d\n", res); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1394 (bit for inverse number)