HDU 1394-minimum inversion Number (BIT)

Source: Internet
Author: User

Test instructions

Give you a sequence of n numbers each time you put the first number at the end to get a new sequence there are n sequences to find which sequence in these sequences contains the smallest total number of reverse order (the minimum total number of reverse order)

Analysis:

Using bit to find out the inverse number of the initial number, the first number is put in reverse order, and the positive sequence of the order.

#include <map>#include<Set>#include<list>#include<cmath>#include<queue>#include<stack>#include<cstdio>#include<vector>#include<string>#include<cctype>#include<complex>#include<cassert>#include<utility>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespaceStd;typedef pair<int,int>Pii;typedefLong Longll;#defineLson l,m,rt<<1#definePi ACOs (-1.0)#defineRson m+1,r,rt<<11#defineAll 1,n,1#defineRead Freopen ("In.txt", "R", stdin)#defineN 5010Constll infll =0x3f3f3f3f3f3f3f3fll;Const intinf=0x7ffffff;Const intMoD =1000000007;intBit[n],a[n],n;intSumintx) {    intnum=0;  while(x>0) {num+=Bit[x]; X-= (x& (-x)); }    returnnum;}voidAddintXintd) {     while(x<=N) {Bit[x]+=D; X+ = (x& (-x)); }}intMain () { while(~SCANF ("%d",&N)) {         for(intI=0; i<n;++i) {scanf ("%d",&A[i]); } memset (bit,0,sizeof(bit)); intTotal=0;  for(inti=n-1; i>=0; i--) { Total+=sum (a[i]+1); Add (A[i]+1,1); }        intminv=Total ;  for(intI=0; i<n;++i) { Total+=n-1-2*A[i]; MINV=min (minv,total); } printf ("%d\n", MINV); }return 0;}

HDU 1394-minimum inversion Number (BIT)

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.