HDU 1394 Minimum Inversion Number (segment tree or tree-like array)

Source: Internet
Author: User

Test instructions

Give you n number, n number is a whole arrangement of 0~n-1.

The minimum value required to count all of its forms in reverse order. All of its forms mean that the first number at the beginning of the array is constantly placed in the last face of the array.

Inverse pairs: i<j and Ai>aj

Ideas:

A tree array is also available,

Look at the code

Code:

Const intMAXN =50005;intsum[maxn<<2];intN;voidPushup (intRT) {Sum[rt]= sum[rt<<1] + sum[rt<<1|1];}voidBuildintLintRintRT) {Sum[rt]=0; if(L==R)return; intm = (L + r) >>1;    Build (Lson); Build (Rson);}voidUpdateintPintLintRintRT) {    if(l==R) {        ++Sum[rt]; return; }    intm = (L + r) >>1; if(P <=m) update (P,lson); ElseUpdate (P,rson); Pushup (RT);}intQueryintLintRintLintRintRT) {    if(L<=l && r<=R) {        returnSum[rt]; }    intm = (L + r) >>1; intRET =0; if(L <= m) ret + =query (L,r,lson); if(R > m) ret + =query (L,r,rson); returnret;}intMain () {intA[MAXN];  while(SCANF ("%d", &n)! =EOF) {Build (0, N-1,1); intsum =0;  for(intI=1; i<=n;++i) {scanf ("%d",&A[i]); Sum+ = Query (a[i]+1, N-1,0, N-1,1); Update (A[i],0, N-1,1); }        //printf ("%d\n", sum);        intAns =sum;  for(intI=1; i<n;++i) {Sum+ = (N-a[i]-1-A[i]); Ans=min (ans,sum); } printf ("%d\n", ans); }}

HDU 1394 Minimum Inversion Number (segment tree or tree-like array)

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.