The number of exchanges bubble sort (Fenwick tree)

Calculate the number of exchanges bubble sort:

Inverse number concept: In one arrangement, if the longitudinal position of a pair of numbers in reverse order of magnitude, i.e. the number greater than the number in front of the latter, they are then referred to as a reverse

All total a reverse arrangement is called this number in reverse order. So bubble sort that is the end of all the numbers in reverse order 0

Ideas :

Violence: We may open a vis [] array record while traversing the i-th bit value of what has occurred. Then traversed (the value of the i-th bit) arr [i], the number appears to give less than ARR [i] (i.e., the number i <j && arr [i] <= arr [j]) of

Then use the current length i is subtracted to obtain the count of the hit condition ARR [i] is the number of reverse.

Optimization: Because each of the elements appear to be less than arr [i] traversal, we hope to open a record array arr [i] appear before the count of the condition (so you do not always take time to traverse request)

Then for single-point update, and seek interval, we can help Fenwick tree data structure. Therefore, to optimize on the use of Fenwick tree. 

operating:

Number of columns of each species is obtained inverse number summation
traverse a [i], a [i ] then bit [i] plus the value of a corresponding
record is less than the value itself before each occurrence, frequency and itself appears
traversed when a j [j] with a front number j, i.e., less than the number itself has a 'bit [j] th
inverse number of j - bit [j]

// the number of exchanges calculated bubble sort:
 // inverse number concept, i.e. the end of the bubble sort is to reverse all of the numbers 0
 @ ideas: Species Number of obtaining the number of columns every number in reverse order of summation
 // traverse a [i ], a [i] then bit [i] plus the value of a corresponding
 // record itself before each value occurs less than, their occurrences
 // a [j has a number j] j prior to traversal, i.e., less than their there are number of bit [j] a
 @ number of reverse J - bit [j] 
#include <bits / STDC ++ H.> #define the iOS iOS :: sync_with_stdio (0); cin.tie (0);
 #define MP the make_pair
 #define the Accept 0
 the using namespace STD; 
typedef Long Long LL; 
typedef unsigned Long Long ULL; 
typedef pair < int , int
   > pii;
const double Pi = acos(-1.0);
const double esp = 1e-9;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5+7;
const int maxm = 1e6+7;
const int mod = 1e9+7;
const int MAXL = 1e6+7;

int n;
int bit[maxn];
int arr[maxn];
int vis[maxn]; 
//That sum from the beginning i, which is the least significant bit of the binary 1 is removed until becomes 0
 // prior item i and 
int SUM ( int i) {
     int S = 0 ;
     the while (i> 0 ) { 
        S + = 'bit [i]; 
        i - = i & - i; 
    } 
    return S; 
} 
// single point with the new, increased the i-th bit x, the minimum non-zero power corresponding to the power applied to 
void the Add ( int i, int X) {
     // 'bit [I] = X; modifying single point
     // I = I + & -i; 
    the while (I <= n-) { 
        ' bit [I] + = X; 
        I+= i & -i;
    }
}
void solve(){
    int ans = 0;
    for(int j = 0;j<n;j++){
        ans += j - sum(arr[j]);
        add(arr[j],1);
    }
//暴力的解法
// for(int i=0;i<n;i++){ // int cnt = 0; // for(int j=0;j<arr[i];j++){ // if(vis[j]) cnt++; // } // ans += i - cnt; // vis[arr[i]] =1; // } printf("%d\n",ans); } int main(){ memset(vis,0,sizeof(vis)); scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&arr[i]); } solve(); return 0; }

Guess you like

Origin www.cnblogs.com/Tianwell/p/11490987.html