Blue Bridge - Past questions children queuing ------ Fenwick tree

Problem Description

  n th child standing in a row. Now, arrange them from low to high in order of height, but you can only exchange position adjacent two children.

  Each child has a degree of upset. Initially, the degree of upset that all children are zero.

  If one were asked to exchange the children for the first time, he's not happy 1 degree increase, if the second exchange asked him if he's unhappy degree increased by 2 (that is, the degree of upset 3), and so on. When a child requires k-th exchange, his unhappy increasing degree k.

  Excuse me, let all the children line up from low to high, they are not happy and the minimum degree of how much.

  If you have two children the same height, who is standing in front of them who are not related.

Input Format

  Comprising a first line of input integer n, the number of children.
  The second line contains n integers H1 H2 ... Hn, respectively, each child's height.

Output Format

  An output line, comprising an integer, and indicates the minimum degree of upset of children.

Sample input

3
3 2 1

Sample Output

9

Sample Description

  Exchanging first height for children 2 and 3, and then exchange the height of children 3 and 1, and then exchange the height of children 1 and 2, the degree of each child is happy 3, the sum of 9.

Scale data and conventions

  For 10% of the data, 1 <= n <= 10 ;
  for 30% of the data, 1 <= n <= 1000 ;
  for 50% of data, 1 <= n <= 10000 ;

  To 100% of the data, 1 <= n <= 100000,0 <= Hi <= 1000000.

 

Problem-solving ideas:

Features Fenwick tree is very fast demand and the first n elements, and we pray that this reverse of what does it matter?

Here, we look to subtly change shape, or the data subject is given as an example 321.

Because Fenwick tree is actually composed of two parts: an array of statistical data array +, we look at the data array. Because Fenwick tree starting from 1, and the title Xiaopen friends height can be 0 (really long exposure), so we see each little friends of height and then add 1 as a subscript Fenwick tree will value 1 to the corresponding memory location.

3 is read first, the amount of data read at this time is one, this becomes

C[1]    C[2]     C[3]       C[4]       C[5]        C[6]     C[7]     C[8]

 0      0      0           1         0         0       0       0

See sum (C [1], C [4]) = 1 (statistic array can be obtained from the tree-array), the number of digits is smaller than or equal to 3, that is, when the input first digit 3 when there is no small number than it is present, then the total number of input digital we -sum (C [1], C [4]) = 0, that is greater than the number of digits is 3 0, we set b [ 0] = 0.

 

2 the second read, the data amount read at this time is two, this becomes

C[1]     C[2]     C[3]        C[4]       C[5]      C[6]         C[7]         C[8]

  0       0       1        1          0         0           0         0

See sum (C [1], C [3]) = 1, then it is smaller than any of the number does not exist, but the amount of data input at this time is 2, and 2-1 = 1, that is, the presence of 2 and before a number greater than 2, of course, is the number 3, we other b [1] = 1.

 

A third reading, the data amount read at this time is 3, this becomes

C[1]   C[2]   C[3]   C[4]   C[5]    C[6]    C[7]   C[8]

 0    1     1     1     0       0      0     0

See sum (C [1], C [2]) = 1, then it is smaller than any of the number does not exist, but the amount of data when the input is 3, and 3-1 = 2, that is, the presence of before two numbers 1 and greater than 1, of course, is the number 3, we other b [2] = 2.

 

This, we have calculated the number of large number of the number of each of the foregoing, there is a data NUM [], the Now we turn first insert 1, 2 and then inserted, then insert 3, but this time we is a natural number of not subtracting the sum by the total number, while the direct sum seek, seeking out smaller number after each number, then the resulting accumulated value corresponding to the b [i], the final we will be b [0] = 2, b [1] = 2, b [2] = 2, respectively num [0] = 3, num [1] = 2, num [2] = 1.

 

To obtain the number of times each little friends are moving, we need to calculate the degree of upset, in fact, here you can make a table in advance, it is to be moved unhappy value n times after a full work out, and then directly on it here, we will save it to total [] array, and the total [2] = 3, so the total value is 9 upset.

 

Note that if duplicate numbers appear how to do, if there is, in fact, the problem will be part of a larger request that the number before each number, because the number of the total number of uses, if the same number appeared, will lead to the result of the subtraction is too large, but also happens to be a large amount of duplication -1, then we can calculate the amount of repeat, then subtract this part of the line, the key is how to calculate the amount of repetition, actually very simple, through the trees shaped array, we can find the sum (1, a) and the sum (1, a + 1), wherein the input is a number, a number less than the number of the former is calculated, which is calculated in a less the counted number, a minus two is equal to the number of a.

 

Fenwick tree --------- assumed array a [1..n], then query a [1] + ... + [n] a log of the time level, and a data line structure, support the right to modify the value of an element of complexity also log level.

To observe this map

  In fact, it is quite difficult to understand in this figure, simply Fenwick tree is a way to skip a grade;

  such as: 

  

When the array a [0] into the inside, according to the following code, x + = lowbit (x), which is based on the principle of the inverted complement and, when x is 1, and a [1] in this case add an , and then x becomes 2, a [2] is also a plus, when x> MAX, the end of the function, so it is a closer look at the process of climbing up.

lowbit int (int X) {
    return & the -X-X; // this is a (1) = 1 + 1 == accordance with the principles and radix complement 1 + = lowbie; binary truth which is present, in the end find a binary 0 number;
}
void the Add (int X) {
    the while (X <= MAX) {
        T [X] ++;
        X = lowbit + (X);
    }
}

The following is a summation

int sum(int x){
    int n=0;
    while(x>0){
        n+=t[x];
        x-=lowbit(x);
    }

 

When x = 3, x- = lowbit (x), x = 2; x becomes 0 recalculation; 2 because of hexadecimal 10, after the above bitlow according to any course 2, s so that x = 2 -2 = 0; out of the loop, which is the following reasons, we had better do the math step by step, to find the law.

 

Assuming that the array a [1..n], then query a [1] + ... + [n] a time is log level, and is an online data structure to support the right to modify the value of an element, complex degree also log level.

To observe this figure:

Make this tree node numbers of C1, C2 ... Cn. The sum of the value of the tree so that the value of each node, it is easy to find:

C1 = A1

C2 = A1 + A2

C3 = A3

C4 = A1 + A2 + A3 + A4

C5 = A5

C6 = A5 + A6

C7 = A7

C8 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8

...

C16 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8 + A9 + A10 + A11 + A12 + A13 + A14 + A15 + A16

Here's an interesting property:

Set node number is x, then the jurisdiction of the node interval is 2 ^ k (where k x is the number of binary 0's end) elements. Because this interval is the last element of necessity Ax,

So it is obvious: Cn = A (n - 2 ^ k + 1) + ... + An

code show as below:

 

#include<cstdio>

#include <the iostream>
#include <CString>
#define MAX 1000010
the using namespace STD;
int T [MAX] = {0}, A [100010] = {0}, B [100010] = {0}, n-;
Long Long Total [100000] = {0};
int lowbit (int X) {
    return X & the -X-;
}
void the Add (int X) {
    the while (X <= MAX) {
        T [X] ++;
        X + = lowbit (X) ;
    }
}
int SUM (int X) {
    int = n-0;
    the while (X> 0) {
        n-+ T = [X];
        X-lowbit = (X);
    }
    return n-;
}


// Note that the array tree 0 marked as part of the children's height, but can not (never thought) to zero so the data once entered on their plus one! !
int main () {
    scanf("%d",&n);
    for(int i=1;i<n;i++)
    total[i]=total[i-1]+i;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        add(a[i]+1);
        b[i]=i-sum(a[i]+1);
    }
    memset(t,0,sizeof(t));
    long long ans=0;
    for(int i=n;i>0;i--){
        b[i]+=sum(a[i]);
        ans+=total[b[i]];
        add(a[i]+1);
    }
    printf("%lld\n",ans);
    return 0;
}

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_37807989/article/details/79512902