樹狀數(shù)組,具體的說是 離散化+樹狀數(shù)組。這也是學(xué)習(xí)樹狀數(shù)組的第一題.
算法的大體流程就是:
1.先對輸入的數(shù)組離散化,使得各個元素比較接近,而不是離散的,
2.接著,運用樹狀數(shù)組的標(biāo)準(zhǔn)操作來累計數(shù)組的逆序數(shù)。
算法詳細解釋:
1.解釋為什么要有離散的這么一個過程?
剛開始以為999.999.999這么一個數(shù)字,對于int存儲類型來說是足夠了。
還有只有500000個數(shù)字,何必要離散化呢?
剛開始一直想不通,后來明白了,后面在運用樹狀數(shù)組操作的時候,
用到的樹狀數(shù)組C[i]是建立在一個有點像位存儲的數(shù)組的基礎(chǔ)之上的,
不是單純的建立在輸入數(shù)組之上。
比如輸入一個9 1 0 5 4,那么C[i]樹狀數(shù)組的建立是在,
下標(biāo) 0 1 2 3 4 5 6 7 8 9
數(shù)組 1 1 0 0 1 1 0 0 0 1
現(xiàn)在由于999999999這個數(shù)字相對于500000這個數(shù)字來說是很大的,
所以如果用數(shù)組位存儲的話,那么需要999999999的空間來存儲輸入的數(shù)據(jù)。
這樣是很浪費空間的,題目也是不允許的,所以這里想通過離散化操作,
使得離散化的結(jié)果可以更加的密集。
2. 怎么對這個輸入的數(shù)組進行離散操作?
離散化是一種常用的技巧,有時數(shù)據(jù)范圍太大,可以用來放縮到我們能處理的范圍;
因為其中需排序的數(shù)的范圍0---999 999 999;顯然數(shù)組不肯能這么大;
而N的最大范圍是500 000;故給出的數(shù)一定可以與1.。。。N建立一個一一映射;
①當(dāng)然用map可以建立,效率可能低點;
②這里用一個結(jié)構(gòu)體
struct Node
{
int v,ord;
}p[510000];和一個數(shù)組a[510000];
其中v就是原輸入的值,ord是下標(biāo);然后對結(jié)構(gòu)體按v從小到大排序;
此時,v和結(jié)構(gòu)體的下標(biāo)就是一個一一對應(yīng)關(guān)系,而且滿足原來的大小關(guān)系;
for(i=1;i<=N;i++) a[p[i].ord]=i;
然后a數(shù)組就存儲了原來所有的大小信息;
比如 9 1 0 5 4 ------- 離散后aa數(shù)組就是 5 2 1 4 3;
具體的過程可以自己用筆寫寫就好了。
3. 離散之后,怎么使用離散后的結(jié)果數(shù)組來進行樹狀數(shù)組操作,計算出逆序數(shù)?
如果數(shù)據(jù)不是很大, 可以一個個插入到樹狀數(shù)組中,
每插入一個數(shù), 統(tǒng)計比他小的數(shù)的個數(shù),
對應(yīng)的逆序為 i- getsum( aa[i] ),
其中 i 為當(dāng)前已經(jīng)插入的數(shù)的個數(shù),
getsum( aa[i] )為比 aa[i] 小的數(shù)的個數(shù),
i- sum( aa[i] ) 即比 aa[i] 大的個數(shù), 即逆序的個數(shù)
但如果數(shù)據(jù)比較大,就必須采用離散化方法
假設(shè)輸入的數(shù)組是9 1 0 5 4, 離散后的結(jié)果aa[] = {5,2,1,4,3};
在離散結(jié)果中間結(jié)果的基礎(chǔ)上,那么其計算逆序數(shù)的過程是這么一個過程。
1,輸入5, 調(diào)用upDate(5, 1),把第5位設(shè)置為1
1 2 3 4 5
0 0 0 0 1
計算1-5上比5小的數(shù)字存在么? 這里用到了樹狀數(shù)組的getSum(5) = 1操作,
現(xiàn)在用輸入的下標(biāo)1 - getSum(5) = 0 就可以得到對于5的逆序數(shù)為0。
2. 輸入2, 調(diào)用upDate(2, 1),把第2位設(shè)置為1
1 2 3 4 5
0 1 0 0 1
計算1-2上比2小的數(shù)字存在么? 這里用到了樹狀數(shù)組的getSum(2) = 1操作,
現(xiàn)在用輸入的下標(biāo)2 - getSum(2) = 1 就可以得到對于2的逆序數(shù)為1。
3. 輸入1, 調(diào)用upDate(1, 1),把第1位設(shè)置為1
1 2 3 4 5
1 1 0 0 1
計算1-1上比1小的數(shù)字存在么? 這里用到了樹狀數(shù)組的getSum(1) = 1操作,
現(xiàn)在用輸入的下標(biāo) 3 - getSum(1) = 2 就可以得到對于1的逆序數(shù)為2。
4. 輸入4, 調(diào)用upDate(4, 1),把第5位設(shè)置為1
1 2 3 4 5
1 1 0 1 1
計算1-4上比4小的數(shù)字存在么? 這里用到了樹狀數(shù)組的getSum(4) = 3操作,
現(xiàn)在用輸入的下標(biāo)4 - getSum(4) = 1 就可以得到對于4的逆序數(shù)為1。
5. 輸入3, 調(diào)用upDate(3, 1),把第3位設(shè)置為1
1 2 3 4 5
1 1 1 1 1
計算1-3上比3小的數(shù)字存在么? 這里用到了樹狀數(shù)組的getSum(3) = 3操作,
現(xiàn)在用輸入的下標(biāo)5 - getSum(3) = 2 就可以得到對于3的逆序數(shù)為2。
6. 0+1+2+1+2 = 6 這就是最后的逆序數(shù)
分析一下時間復(fù)雜度,首先用到快速排序,時間復(fù)雜度為O(NlogN),
后面是循環(huán)插入每一個數(shù)字,每次插入一個數(shù)字,分別調(diào)用一次upData()和getSum()
外循環(huán)N, upData()和getSum()時間O(logN) => 時間復(fù)雜度還是O(NlogN).
最后總的還是O(NlogN).
poj 2299 :
| 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 Ultra-QuickSort produces the output
Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.
Input
Output
Sample Input
5 9 1 0 5 4 3 1 2 3 0
Sample Output
6 0
Source
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 |
#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]; //離散化后的數(shù)組 int c[maxn]; //樹狀數(shù)組 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; //樹狀數(shù)組求逆序 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; } |
對于一個數(shù)組{5,3,7,4,6,2,1,8},如果我們以中間(或者左中)為界限將其分成兩個數(shù)組,只要知道這兩個數(shù)組中的逆序?qū)?shù)和兩邊的數(shù)所組成的逆序?qū)?shù)就是它所有的逆序?qū)?shù)了。而數(shù)組劃分的遞歸結(jié)束點在兩個數(shù)組都只有一個元素的時候,因此兩數(shù)組中的逆序?qū)?shù)可以通過遞歸求得。要去求分裂的兩數(shù)組中的逆序?qū)?shù)可以這樣去做:先將兩數(shù)組按升序排序,若前一個數(shù)組的第一個元素比后一個的大,則說明前一組的后面所有都要比后一組的第一個元素大,這樣將較小的第一個元素去掉后,繼續(xù)比較兩個數(shù)組的第一個元素可以把所有的逆序?qū)φ?/span>到。
下面是該算法的JAVA實現(xiàn):
import java.util.*;
public class CountInversedNumbers{
public static int count=0; //記錄逆序?qū)€數(shù);
public void sort_count(int []L){
int n=L.length;
int a=(int)(n/2+0.5);
int b=(int)(n/2);
int A[]=new int[a];
int B[]=new int[b];
for (int i=0;i< a;i++)
A[i]=L[i];
for (int j=0;j< b;j++)
B[j]=L[j+a]; //把單個數(shù)組劃分成兩個數(shù)組;
if(a>1&&b>1) {
sort_count(A);
sort_count(B); //遞歸的劃分數(shù)組;
}
Arrays.sort(A);
Arrays.sort(B); //將數(shù)組排序;
merge_count(A,B);
}
public void merge_count(int []M, int[]N){ //求被劃分的兩個數(shù)組中元素組成的逆序?qū)?
int c=M.length;
int d=N.length;
while(c>0&&d>0){
if(M[0]>N[0]) {
this.count=this.count+c;
for(int i=0;i< d-1;i++)
N[i]=N[i+1];
d--;
} else{
for(int j=0;j< c-1;j++)
M[j]=M[j+1];
c--;
}
}
}
public static void main(String[] args){
int K[]={6,2,3,5,7,1,4,8};
CountInversedNumbers a=new CountInversedNumbers();
a.sort_count(K);
System.out.println(a.count); //輸出結(jié)果是11;
}
}
#include<iostream>
#include<vector>
#include<fstream>
#include<iterator>
using namespace std;
const int inf = 2147483647;
int count = 0;
//get the index of the first element larger than val on [beg, end)
int upperbound(int *ia, int beg, int end, int val)
{
if (beg + 1 == end) return ia[beg] > val ? beg : -1;
int mid = beg + (end - beg) / 2;
if (ia[mid - 1] <= val)
return upperbound(ia, mid, end, val);
return upperbound(ia, beg, mid, val);
}
void merge(int *ia, int p, int q, int r)
{
for (int i = p, j = q + 1; j <= r; ++j)
if ((i = upperbound(ia, i, q + 1, ia[j])) != -1)
count += q - i + 1;
else
break;
int n1 = q - p + 1, n2 = r - q;
int *left = new int[n1 + 1], *right = new int[n2 + 1];
for (int i = 0; i != n1; ++i)
left[i] = ia[p + i];
for (int i = 0; i != n2; ++i)
right[i] = ia[q + i + 1];
left[n1] = inf;
right[n2] = inf;
int i = 0, j = 0, k = p;
while (k <= r)
if (left[i] <= right[j])
ia[k++] = left[i++];
else
ia[k++] = right[j++];
delete [] left;
delete [] right;
}
void inversionCount(int *ia, int p, int r)
{
if (p < r) {
int q = (p + r) / 2;
inversionCount(ia, p, q);
inversionCount(ia, q + 1, r);
merge(ia, p, q, r);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
ifstream cin("a.txt");
for (int n; cin >> n && n; ) {
int *ia = new int[n];
for (int i = 0; i != n && cin >> ia[i]; ++i);
inversionCount(ia, 0, n - 1);
cout << count << endl;
count = 0;
delete [] ia;
}
return 0;
}
http://www.cnblogs.com/justforgl/archive/2012/07/27/2612364.html





