01_ Merge sort for reverse order number (example: Blue Bridge cup-children queue)

Source: Internet
Author: User

Source: Fifth session of the Blue Bridge Cup preliminary B/C + + undergraduate Group 10th question

problem Description:n Children, height of h1,h2,... HK,..., HN, stand in a row, now to the height from low to high order, but each can only exchange location adjacent to two children. Every child has a degree of upset. At the beginning, all the children's unhappy degree is 0. If a child is asked to exchange for the first time, his degree of upset increases by 1, and if he is asked to exchange for the second time, his degree of upset increases by 2 (ie, 3), and so on. When a child is asked to exchange for K, his degree of upset is increased by K. Ask for all children to row from low to high, their level of upset and the minimum is how much (if there are two children of the same height, then they who stand in front of who is not related).

Problem Analysis: 1. for any child of N children K, the analysis can be:

          A. The children in front of K higher than K will be exchanged to the back of K (the number of children is recorded as high),

          B. The children behind K are exchanged to K before they go (the number of children is low).

So at the end of the order, K at least swap high+low times.

       2. first assume that n children are not equal in height, we can achieve the high value of each child by merging sorting, set the child k on the K position, is the lower of the W, then low = W-1-(k-1-high). (W-1 says the total number is lower than K, K-1-high indicates the number of people in front of K lower than k)

       3. Merge sort to find the high value of the child K, the initial time high[k]=0, in the height array a[] a[l]~a[mid] and A[mid+1]~a[r] Merge operation, if the A[l]~a[mid] in a number a[j], if the mid +1<=k<=r && A[k]<a[j], then high[k] + + mid-j+1 (A[j]~a[mid] is larger than a[k] and in the front of A[k], so the accumulation)

       4. now deal with n children, the height is equal, the conversion to height is all unequal:

          A. The child's height is renumbered from the 1~n in order from small to large, and for children of equal height, numbered from left to right in order from the 1~n (this does not affect the result).

          B. Renumber method: First assign all the children's height array (a[]) to another array (b[]) and sort (from small to large), for each a[i], go to b[] inside to find its relative position K (subscript), and make B[k]--。 One of the two points returned is the leftmost a[i]==b[k] subscript k,b[k]--is guaranteed to be checked again when B[k] is found to be the latter (this will not affect b[k] in front of the number).

Example Link:http://lx.lanqiao.org/problem.page?gpid=T123 (login required)

Examples:

Previous questions children line upTime limit: 1.0s memory limit: 256.0MB Problem DescriptionN Children stand in a row. Now it's time to put them in the order of height from low to high, but only two children are exchanged at each location.
Every child has a degree of upset. At the beginning, all the children's unhappy degree is 0.
If a child is asked to exchange for the first time, his degree of upset increases by 1, and if he is asked to exchange for the second time, his degree of upset increases by 2 (ie, 3), and so on. When a child is asked to exchange for K, his degree of upset is increased by K.
Ask, to let all children in line from low to high, their degree of upset and minimum is how much.
If two children are of the same height, it is no matter who they stand in front of. Input FormatThe first line of input contains an integer n, which indicates the number of children.
The second line consists of n integers H1 H2 ... Hn the height of each child, respectively. output FormatThe output line contains an integer that indicates the child's degree of upset and the minimum value. Sample Input3
3 2 1 Sample Output9 Sample DescriptionFirst of all, exchange height of 3 and 2 children, and then exchange height of 3 and 1 children, and then exchange height of 2 and 1 children, each child's unhappy degree is 3, the sum of 9. Data size and conventions for 10% of data, 1<=n<=10;
For 30% of data, 1<=n<=1000;
For 50% of data, 1<=n<=10000;
For 100% of data, 1<=n<=100000,0<=hi<=1000000.

Code:

1#include"stdio.h"2#include"string.h"3#include"algorithm"4 using namespacestd;5 #defineN 1000056 7 intN;8 intA[n],b[n];9 Ten structnode One { A     intI//num[k].i The position of the child in the team -     intLow//Num[k].low The number of children behind the K-high children's lower than K -     intHigh//Num[k].high The number of children in front of the high child in the higher than the K the }num[n]; -  - intErfen (int*c,intK//Returns the subscript of the first and K equal numbers in the C array - { +     intL=1, r=N,mid; -      while(l<R) +     { AMid = (l+r)/2; at         if(c[mid]==k) -         { -             if(c[mid-1]<k) -                 returnmid; -             Else -r=mid-1; in         } -         Else if(c[mid]>k) tor=mid-1; +         Else -L=mid+1; the     } *     returnl; $ }Panax Notoginseng  - voidMerge (int*a,intLintR) the { +     if(L==R)return ; A     intI,j,k,mid; theMid = (l+r)/2; + Merge (a,l,mid); -Merge (a,mid+1, R); $      for(i=l,j=mid+1, k=0; I<=mid && j<=R;) $     { -         if(a[i]<=A[j]) -b[k++] = a[i++]; the         Else if(a[i]>A[j]) -b[k++] = A[j],num[a[j]].high + = mid-i+1, J + +;Wuyi     } the      while(i<=mid) -b[k++] = a[i++]; Wu      while(j<=R) -b[k++] = a[j++]; About      for(i=l,j=0; i<=r; i++) $A[i] = b[j++]; - } -  - intMain () A { +     inti,k,w; the     Long Longans,t; -a[0]=-1; $      while(SCANF ("%d", &n)! =EOF) the     { the          for(i=1; i<=n; i++) theNum[i].high =0; the          for(i=1; i<=n; i++) -         { inscanf"%d",&a[i]); theB[i] =A[i]; the         } AboutSort (b +1, b+n+1); the          for(i=1; i<=n; i++)//re-numbering the a[] the         { theK =Erfen (B,a[i]); +A[i] =K; -b[k]--; theNUM[A[I]].I =i;Bayi         } theMerge (A,1, n);//Merge and Beg all children's high theAns =0; -          for(i=1; i<=n; i++) -         { theK = num[i].i;//k denotes the position of the low child in the team thew = i;//W means the lower of the W theNum[i].low = W1-(K-1-Num[i].high); thet = Num[i].high +Num[i].low; -Ans + = t* (t+1)/2; the         } theprintf"%i64d\n", ans); the     }94     return 0; the}

01_ Merge sort for reverse order number (example: Blue Bridge cup-children queue)

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.