Ultra-QuickSort POJ - 2299 树状数组求逆序对

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

简单的树状数组应该 应该是最基本的应用求逆序对
注意离散化
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <set>
 7 #include <iostream>
 8 #include <map>
 9 #include <stack>
10 #include <string>
11 #include <vector>
12 #define pi acos(-1.0)
13 #define eps 1e-6
14 #define fi first
15 #define se second
16 #define lson l,m,rt<<1
17 #define rson m+1,r,rt<<1|1
18 #define bug         printf("******\n")
19 #define mem(a,b)    memset(a,b,sizeof(a))
20 #define fuck(x)     cout<<"["<<x<<"]"<<endl
21 #define f(a)        a*a
22 #define sf(n)       scanf("%d", &n)
23 #define sff(a,b)    scanf("%d %d", &a, &b)
24 #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
25 #define pf          printf
26 #define FRE(i,a,b)  for(i = a; i <= b; i++)
27 #define FREE(i,a,b) for(i = a; i >= b; i--)
28 #define FRL(i,a,b)  for(i = a; i < b; i++)
29 #define FRLL(i,a,b) for(i = a; i > b; i--)
30 #define FIN freopen("DATA.txt","r",stdin)
31 #define lowbit(x)   x&-x
32 #pragma comment (linker,"/STACK:102400000,102400000")
33 
34 using namespace std;
35 typedef long long LL ;
36 const int maxn = 5e5 + 10;
37 int n, a[maxn], c[maxn], b[maxn], bit[maxn];
38 void update(int x) {
39     while(x <= n) {
40         bit[x] += 1;
41         x += lowbit(x);
42     }
43 }
44 int sum(int i) {
45     int ret = 0;
46     while(i > 0) {
47         ret += bit[i];
48         i -= lowbit(i);
49     }
50     return ret;
51 }
52 int main() {
53     while(scanf("%d", &n), n) {
54         for (int i = 1 ; i <= n ; i++) {
55             scanf("%d", &a[i]);
56             b[i] = a[i];
57         }
58         sort(b + 1, b + 1 + n);
59         for (int i = 1 ; i <= n ; i++)
60             c[i] = lower_bound(b + 1, b + 1 + n, a[i]) - b;
61         mem(bit, 0);
62         LL ans=0;
63         for (int i=1 ;i<=n ;i++) {
64             update(c[i]);
65             ans+=i-sum(c[i]);
66         }
67         printf("%lld\n",ans);
68     }
69     return 0;
70 }

 



转载于:https://www.cnblogs.com/qldabiaoge/p/9415276.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值