A tree-like array for reverse order

Source: Internet
Author: User

1 , what is the number of reverse order?  2.use a tree array to find the number of         reverse order number 2. 1 meaning         of the tree array in this context 2. 2 How to use a tree array to find the total number of reverse order numbers          2.3 C + + implementation code
View Code

1, what is the number of reverse order?

In one arrangement, if the front and back positions of a pair of numbers are opposite to the size order, that is, the previous number is greater than the subsequent number, then they are called an inverse. The total number of reverse order in a permutation is the number of reverse order in this arrangement.

2. Using a tree array to find the total number of reverse order numbers

2.1 Meaning of the tree array in this context

We assume an array of a[n], when a[n]=0 indicates that the number n does not appear in the sequence, a[n]=1 indicates that the number n appears in the sequence. A corresponds to a tree array of c[n], then c[n] corresponds to maintaining the contents of the array a[n], that is, the tree array C can be used to find the value of an interval in a.

The insertion function of a tree array (assuming void insert (int i,int x)) means: In the case of inverse number, our insertion function is usually used as insert (I, 1), the value of the array a[i] is added 1 (a array should begin initialized to 0, So it can also be understood that the value of setting a[I] is 1, which will add the number I to the meaning of the sequence). , while maintaining the value of the C array.

The interval summation function in a tree array (assuming that the function is defined as: int getsun (int i)) means that the function is used to find the number of elements in the sequence that are less than or equal to the number I. This is obvious, because the tree array C maintains the value of the array a, then the SUM function is used for the subscript is less than or equal to the sum of the array A, and the value of the element in array A is either 0 or 1, so the last to find is less than equals I of the number of elements.

Therefore, it is required that the number of numbers larger than element A in the sequence can be I-getsum (a) (I means the number of elements in this sequence).

2.2 How to use a tree array to find the total number of reverse order

First look at how to reduce the size of the problem:

To find a sequence a B c D, the number of reverse-order number, it can be understood to first find the number of reverse number of a B C K1, and then add a number D after the sequence, to find that the sequence of D before the number of elements less than D K2, then K1+K2 is the sequence a B c d the number of reverse.

Give an example to illustrate:

Assuming that the given sequence is 4 3 2 1, we enter the given sequence from left to right, and each time we enter a number temp, the number of elements greater than temp in the current sequence is computed and added to ans, and the last Ans is the number of reverse orders for this sequence.

Change of sequence (underscore for newly added element)

The number of numbers in the sequence that are greater than the newly added

Operation

{ }

0

No number in the sequence is initialized

{4}

0

Add 4 to the sequence to count the number of elements greater than 4 in this sequence

{4 3}

1

Add 3 to the sequence to count the number of elements greater than 3 in this sequence

{4 3 2}

2

Add 2 to the sequence to count the number of elements greater than 2 in this sequence

{4 3 2 1}

3

Add 1 to the sequence to count the number of elements greater than 1 in this sequence

When all the elements are inserted into the sequence, you can get the number of reverse order numbers for the sequence {4 3 2 1} to be 1+2+3=6.

The 2.3 C + + implementation code is as follows:

#include <iostream>#include<string>using namespacestd;#defineN 1010intC[n];intN;intLowbit (inti) {    returni& (-i);}intInsertintIintx) {     while(i<=N) {C[i]+=x; I+=lowbit (i); }    return 0;}intGetsum (inti) {    intsum=0;  while(i>0) {sum+=C[i]; I-=lowbit (i); }     returnsum;}voidoutput () { for(intI=1; i<=n;i++) cout<<c[i]<<" "; cout<<Endl;}intMain () { while(cin>>N) {        intans=0; Memset (c,0,sizeof(c));  for(intI=1; i<=n;i++){            intA; CIN>>A; Insert (A,1); Ans+=i-insert (a);//counts the number of elements in the current sequence that are greater than a} cout<<ans<<Endl; }    return 0;}

--from Panda [http://www.cnblogs.com/xiongmao-cpp/]

A tree-like array for reverse order

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.