HDU 1394 (bit for inverse number)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.