樹狀數組 求逆序數 poj 2299


這里說的很好,把求逆序的步驟說的很明白,我也是看完才懂的,之前自己想了很久就是不明白為什么可以用樹狀數組求逆序

  

轉載:

樹狀數組,具體的說是 離散化+樹狀數組。這也是學習樹狀數組的第一題.

算法的大體流程就是:

1.先對輸入的數組離散化,使得各個元素比較接近,而不是離散的,

2.接着,運用樹狀數組的標准操作來累計數組的逆序數。

算法詳細解釋:

1.解釋為什么要有離散的這么一個過程?

    剛開始以為999.999.999這么一個數字,對於int存儲類型來說是足夠了。

    還有只有500000個數字,何必要離散化呢?

    剛開始一直想不通,后來明白了,后面在運用樹狀數組操作的時候,

    用到的樹狀數組C[i]是建立在一個有點像位存儲的數組的基礎之上的,

    不是單純的建立在輸入數組之上。

    比如輸入一個9 1 0 5 4,那么C[i]樹狀數組的建立是在,

    下標 0 1 2 3 4 5 6 7 8 9

    數組 1 1 0 0 1 1 0 0 0 1

    現在由於999999999這個數字相對於500000這個數字來說是很大的,

    所以如果用數組位存儲的話,那么需要999999999的空間來存儲輸入的數據。

    這樣是很浪費空間的,題目也是不允許的,所以這里想通過離散化操作,

    使得離散化的結果可以更加的密集。

2. 怎么對這個輸入的數組進行離散操作?

   離散化是一種常用的技巧,有時數據范圍太大,可以用來放縮到我們能處理的范圍;

   因為其中需排序的數的范圍0---999 999 999;顯然數組不肯能這么大;

   而N的最大范圍是500 000;故給出的數一定可以與1.。。。N建立一個一一映射;

   ①當然用map可以建立,效率可能低點;

   ②這里用一個結構體

   struct Node

   {

      int v,ord;

   }p[510000];和一個數組a[510000];

   其中v就是原輸入的值,ord是下標;然后對結構體按v從小到大排序;

   此時,v和結構體的下標就是一個一一對應關系,而且滿足原來的大小關系;

   for(i=1;i<=N;i++) a[p[i].ord]=i;

   然后a數組就存儲了原來所有的大小信息;

   比如 9 1 0 5 4 ------- 離散后aa數組就是 5 2 1 4 3;

   具體的過程可以自己用筆寫寫就好了。

3. 離散之后,怎么使用離散后的結果數組來進行樹狀數組操作,計算出逆序數?

    如果數據不是很大, 可以一個個插入到樹狀數組中,

    每插入一個數, 統計比他小的數的個數,

    對應的逆序為 i- getsum( aa[i] ),

    其中 i 為當前已經插入的數的個數,

    getsum( aa[i] )為比 aa[i] 小的數的個數,

    i- sum( aa[i] ) 即比 aa[i] 大的個數, 即逆序的個數

    但如果數據比較大,就必須采用離散化方法

    假設輸入的數組是9 1 0 5 4, 離散后的結果aa[] = {5,2,1,4,3};

在離散結果中間結果的基礎上,那么其計算逆序數的過程是這么一個過程。

1,輸入5,   調用upDate(5, 1),把第5位設置為1

1 2 3 4 5

0 0 0 0 1

計算1-5上比5小的數字存在么? 這里用到了樹狀數組的getSum(5) = 1操作,

現在用輸入的下標1 - getSum(5) = 0 就可以得到對於5的逆序數為0。

2. 輸入2, 調用upDate(2, 1),把第2位設置為1

1 2 3 4 5

0 1 0 0 1

計算1-2上比2小的數字存在么? 這里用到了樹狀數組的getSum(2) = 1操作,

現在用輸入的下標2 - getSum(2) = 1 就可以得到對於2的逆序數為1。

3. 輸入1, 調用upDate(1, 1),把第1位設置為1

1 2 3 4 5

1 1 0 0 1

計算1-1上比1小的數字存在么? 這里用到了樹狀數組的getSum(1) = 1操作,

現在用輸入的下標 3 - getSum(1) = 2 就可以得到對於1的逆序數為2。

4. 輸入4, 調用upDate(4, 1),把第5位設置為1

1 2 3 4 5

1 1 0 1 1

計算1-4上比4小的數字存在么? 這里用到了樹狀數組的getSum(4) = 3操作,

現在用輸入的下標4 - getSum(4) = 1 就可以得到對於4的逆序數為1。

5. 輸入3, 調用upDate(3, 1),把第3位設置為1

1 2 3 4 5

1 1 1 1 1

計算1-3上比3小的數字存在么? 這里用到了樹狀數組的getSum(3) = 3操作,

現在用輸入的下標5 - getSum(3) = 2 就可以得到對於3的逆序數為2。

6. 0+1+2+1+2 = 6 這就是最后的逆序數

分析一下時間復雜度,首先用到快速排序,時間復雜度為O(NlogN),

后面是循環插入每一個數字,每次插入一個數字,分別調用一次upData()和getSum()

外循環N, upData()和getSum()時間O(logN) => 時間復雜度還是O(NlogN).

最后總的還是O(NlogN).

 


 

poj 2299 :

Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 26864   Accepted: 9642

Description

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence 
9 1 0 5 4 ,

Ultra-QuickSort produces the output 
0 1 4 5 9 .

Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

Output

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

Sample Input

5
9
1
0
5
4
3
1
2
3
0

Sample Output

6
0

Source

 

代碼:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std;

const int maxn=500005;
int n;
int aa[maxn]; //離散化后的數組
int c[maxn];    //樹狀數組

struct Node{
   int v;
   int order;
}in[maxn];

int lowbit(int x)
{
    return x&(-x);
}

void update(int t,int value)
{
    int i;
    for(i=t;i<=n;i+=lowbit(i))
    {
        c[i]+=value;
    }
}

int getsum(int x)
{
    int i;
    int temp=0;
    for(i=x;i>=1;i-=lowbit(i))
    {
        temp+=c[i];
    }
    return temp;
}

bool cmp(Node a ,Node b)
{
    return a.v<b.v;
}

int main()
{
    int i,j;
    while(scanf("%d",&n)==1 && n)
    {
        //離散化
        for(i=1;i<=n;i++)
        {
            scanf("%d",&in[i].v);
            in[i].order=i;
        }
        sort(in+1,in+n+1,cmp);
        for(i=1;i<=n;i++) aa[in[i].order]=i;
        //樹狀數組求逆序
        memset(c,0,sizeof(c));
        long long ans=0;
        for(i=1;i<=n;i++)
        {
            update(aa[i],1);
            ans+=i-getsum(aa[i]);
        }
        cout<<ans<<endl;
    }
    return 0;
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM