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

POJ 2299Ultra-QuickSort

使用树状数组记录逆序对数。

把数组按照大小顺序插入,getsum(i)就是i前面的比他大的数。

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4
 5 using namespace std;
 6
 7 const int maxn = 500005;
 8 int reflect[maxn],c[maxn],N;
 9
10 struct Node
11 {
12     int val;
13     int pos;
14     bool operator < (const Node &r) const
15     {
16         return val < r.val;
17     }
18 }node[maxn];
19
20 int lowbit(int x)
21 {
22     return x&(-x);
23 }
24
25 void update(int x)
26 {
27     while(x<=N)
28     {
29         c[x] += 1;
30         x += lowbit(x);
31     }
32 }
33
34 int getsum(int x)
35 {
36     int sum = 0;
37     while(x > 0)
38     {
39         sum += c[x];
40         x -= lowbit(x);
41     }
42     return sum;
43 }
44
45 int main()
46 {
47     while(~scanf("%d",&N) && N)
48     {
49         for(int i=1;i<=N;i++)
50         {
51             scanf("%d",&node[i].val);
52             node[i].pos = i;
53         }
54         sort(node+1,node + N+1);
55         for(int i=1;i<=N;i++) reflect[node[i].pos] = i;
56         memset(c,0,sizeof c);
57
58         long long ans = 0;
59         for(int i=1;i<=N;i++)
60         {
61             update(reflect[i]);
62             ans += i - getsum(reflect[i]);
63         }
64         printf("%lld\n",ans);
65     }
66 }
时间: 2024-08-05 13:19:22

POJ 2299 -Ultra-QuickSort-树状数组求逆序数的相关文章

poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)

题目链接:http://poj.org/problem?id=2299 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

poj 2299 Ultra-QuickSort(树状数组 / 求逆序数)

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 46080   Accepted: 16763 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

POJ 3067 Japan(树状数组/求逆序数)

Japan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22258   Accepted: 5995 Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coas

poj 2299 树状数组求逆序数+离散化

http://poj.org/problem?id=2299 最初做离散化的时候没太确定但是写完发现对的---因为后缀数组学的时候,,这种思维习惯了吧 1.初始化as[i]=i:对as数组按照num[]的大小间接排序 2.bs[as[i]]=i:现在bs数组就是num[]数组的离散化后的结果 3.注意,树状数组中lowbit(i)  i是不可以为0的,0&(-0)=0,死循环... #include <cstdio> #include <cstring> #include

树状数组求逆序数

poj 2299 树状数组求逆序数题目链接:http://poj.org/problem?id=2299 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #include <stack> 8 #include <

ZOJ-2386 Ultra-QuickSort 【树状数组求逆序数+离散化】

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 seque

HDU 1394 Minimum Inversion Number (树状数组求逆序数)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13942    Accepted Submission(s): 8514 Problem Description The inversion number of a given number sequence a1, a2, ..., a

hdu 5147 Sequence II (树状数组 求逆序数)

题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 331    Accepted Submission(s): 151 Problem Description Long long ago, there is a sequence A with length n. All numbers in this se

hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数)

题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m = 0 - the initial seqence)a2, a3, ..., an, a1 (where m = 1)a3, a4, ..., an, a1, a2 (where m = 2)...an, a1, a2, ..., an-1 (where m = n-1)求这些序列中,逆序数最少的

poj2299 Ultra-QuickSort 树状数组求逆序数

poj2299 Ultra-QuickSort   树状数组求逆序数 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 49587   Accepted: 18153 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequenc