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

来源:互联网 发布:贵州旅游大数据报告 编辑:程序博客网 时间:2024/05/16 04:44

原题描述:
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 62187 Accepted: 23121
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
题目大意:
给定你多个序列,问其中的逆序对个数(数据极大!!!)


首先各位同学看到逆序对第一眼想到的应该就是归并排序了吧。
但如果这样编完,会发现自己会T很多。
这里就给大家介绍一种快速求逆序对个数的方法:离散化+树状数组!


1.离散化:
对于一个无序序列,应该很自然就会想到将其排成有序序列离散化,而本题的做法也是这样的。
举例来说,
原本序列:1 7 2 4 3 6
对应位置:1 2 3 4 5 6
离散化以后(以位值为离散化标准)
当前序列:1 2 3 4 5 6
对应位置:1 3 5 4 6 2
代码实现因该也不难:

struct node{int pos,date;}a[500001]; for(i=1;i<=n;i++) {   a[i].pos=i;   a[i].date=read(); }

那么此时我们就将求值的逆序对转化为了求位置的逆序对了。
然后我们再引入一个b数组,其下标表示真实位置,其值表示在刚刚离散化的序列中所处位置。
具体来说,
for(i=1;i<=n;i++)b[a[i].pos]=i;
举例来说,还是刚刚那个例子中的序列,第一遍按位值离散化,再赋给b后,为:
a[1].pos=1;————>b[1]=1;
a[2].pos=3;————>b[3]=2;
a[3].pos=5;————>b[5]=3;
a[4].pos=4;————>b[4]=4;
a[5].pos=6;————>b[6]=5;
a[6].pos=2;————>b[2]=6;
然后将b排序:b[1..6]={1 6 2 4 3 5};
其实b[i]=j表示的就是第i个位置上的为第j大的数。
做到这里,离散化就基本做完了,由于我们的每一步离散化都基于值的大小,所以此时我们可以用b推出某个数前方有多少个小于其的数。(再看下文前读者不妨先独立思考一下)。


2.树状数组:
在我们离散化得到b数组后,我们就要开始利用树状数组求逆序对的个数了。
准确的说,我们利用b所求的为“顺序对的个数”。
而逆序对的个数=总序对个数-顺序对个数。
由于b的下标表示的是当前位置值的大小排名,
所以我们从1—n依次向树状数组中插入b,一定保证了值的大小顺序。
我们记每个插入节点的贡献为1,
那么其实每插入一个b,他的顺序对个数就为它前面所有节点贡献之和。
为什么呢,其实也并不是很难理解。
依旧以上面的例子说明(顺便回顾一下前面的步骤):
原本序列:1 7 2 4 3 6
对应位置:1 2 3 4 5 6
离散化以后(以位值为离散化标准)
当前序列:1 2 3 4 5 6
对应位置:1 3 5 4 6 2
a[1].pos=1;————>b[1]=1;
a[2].pos=3;————>b[3]=2;
a[3].pos=5;————>b[5]=3;
a[4].pos=4;————>b[4]=4;
a[5].pos=6;————>b[6]=5;
a[6].pos=2;————>b[2]=6;
—>b[1]=1,b[2]=6,b[3]=2,b[4]=4,b[5]=3,b[6]=5;
我们用一条线段表示当前插入情况(求线段区间和读者们自己用树状数组实现,这里只解释说明原理):
我们用“—”表示还未插入。
初始状态:— — — — — —

插入b[1]后:
线段状态:1 — — — — —
对应位值:1
此时和为1,构成的顺对个数为1,逆序对个数为i-1=0;

插入b[2]后:
线段状态:1 — — — — 1
对应位值:1 — — — — 2
当前的和为1+1=2,即构成的顺序对个数为2个,逆序对个数为i-2=0;

插入b[3]=2后:
线段状态:1 1 — — — 1
对应b值: 1 3 — — — 2
我们重点分析这个步骤:
对于当前位置,和为2,构成了两个顺序对,而此时插入的为第三个数,本来前方和应该为3,故组成逆序对个数为3-2=1;回过头看,第三个位置上的2组成逆序对是1个。
后面的依次类推。。。。。

为什么会这样,其实稍微想一想就知道了,我们插入的b[i]表示第i个位置上的数,为第b[i]大的数。如果前方全部为顺序对,那么前方和应该为i,而如果少了,就说明本应该比它小的数跑到后面去了,就构成了逆序对。
上面这一部分较难理解,希望读者们多加体会。


最后给出完整的代码:

#include <algorithm>#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <cmath>using namespace std;struct node{int pos,date;}a[500001];int b[500001],t[2000001];long long ans;int n;int gi() {    int w;bool q=1;char c;    while (((c=getchar())<'0'||'9'<c)&&c!='-');    if (c=='-') q=0,c=getchar();    w=c-'0';    while ('0'<=(c=getchar())&&c<='9') w=w*10+c-'0';    return q?w:-w;}inline bool cmp(node a,node b){  return a.date<b.date;}int lowbit(int a){return a&(-a);}void insert(int x,int val){    while(x<=n)    {      t[x]+=val;      x=x+lowbit(x);    }return;}int getsum(int x){  int sum=0;  while(x>0)    {      sum+=t[x];      x=x-lowbit(x);    }  return sum;}int main(){     freopen("A.txt","r",stdin);     int i,j;    while(1)    {      n=gi();      if(n==0)break;      for(i=1;i<=n;i++)      {      a[i].pos=i;      a[i].date=gi();      }      sort(a+1,a+n+1,cmp);      for(i=1;i<=n;i++)b[a[i].pos]=i;      ans=0;      memset(t,0,sizeof(t));      for(i=1;i<=n;i++)      {        insert(b[i],1);        ans+=i-getsum(b[i]);      }      printf("%lld\n",ans);    }    return 0;}

Hahahaha!

阅读全文
0 0
原创粉丝点击