Ultra-QuickSort

本文介绍了一种特殊的排序算法Ultra-QuickSort,并详细解析了如何利用离散化和树状数组计算输入序列的逆序数,以确定排序所需的最小交换次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*Ultra-QuickSort
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.解释为什么要有离散的这么一个过程  
    后面在运用树状数组操作的时候,用到的树状数组C[i]是建立在一个有点像位存储的数组的基础之上的,不是单纯的建立在输入数组之上。  
比如输入一个9 1 0 5 4,那么C[i]树状数组的建立是在,    
数据:9 1 0 5 4  p[i].val  
编号:1 2 3 4 5  p[i].pos = i*************  
sort  
数据:0 1 4 5 9  (p[i].val)
编号:3 2 5 4 1  (p[i].pos)
顺序:1 2 3 4 5   
a[p[i].编号] = 顺序号;**********************        
a[3] = 1<--0;  (a[p[1].pos]=1,其中p[1].pos=3)
a[2] = 2<--1;  (a[p[2].pos]=2,其中p[2].pos=2)
a[5] = 3<--4;  (a[p[3].pos]=3,其中p[3].pos=5)
a[4] = 4<--5;  (a[p[4].pos]=4,其中p[4].pos=4)
a[1] = 5<--9;  (a[p[5].pos]=5,其中p[5].pos=5)     
a[]={ 5 2 1 4 3 }  
新号:1 2 3 4 5  
值  :   
下标 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建立一个一一映射;  
   (1)当然用map可以建立,效率可能低点;  
   (2)这里用一个结构体  
   struct Node  
   {  
      int val,pos;  
   }p[510000];和一个数组a[510000];  
   其中val就是原输入的值,pos是下标;  
   然后对结构体按val从小到大排序;  
   此时,val和结构体的下标就是一个一一对应关系,  
   而且满足原来的大小关系;  
   for(i=1;i<=N;i++)  
       a[p[i].pos]=i;  
   然后a数组就存储了原来所有的大小信息;  
   比如 9 1 0 5 4 ------- 离散后aa数组  
   就是 5 2 1 4 3;  
   具体的过程可以自己用笔写写就好了。  
3. 离散之后,怎么使用离散后的结果数组来进行树状数组操作,计算出逆序数?  
    如果数据不是很大, 可以一个个插入到树状数组中,  
    每插入一个数, 统计比他小的数的个数,  
    对应的逆序为 i- sum( a[i] ),  
    其中 i 为当前已经插入的数的个数,  
    sum( a[i] )为比 a[i] 小的数的个数,  
    i- sum( a[i] ) 即比 a[i] 大的个数, 即逆序的个数  
    但如果数据比较大,就必须采用离散化方法  
    假设输入的数组是9 1 0 5 4, 离散后的结果a[] = {5,2,1,4,3};  
在离散结果中间结果的基础上,那么其计算逆序数的过程是这么一个过程。  
1.输入5,   调用add(5, 1),把第5位设置为1  
1 2 3 4 5  
0 0 0 0 1  
计算1-5上比5小的数字存在么? 这里用到了树状数组的sum5) = 1操作,  
现在用输入的下标1 -sum(5) = 0 就可以得到对于5的逆序数为02. 输入2, 调用add(2, 1),把第2位设置为1  
1 2 3 4 5  
0 1 0 0 1  
计算1-2上比2小的数字存在么? 这里用到了树状数组的sum2) = 1操作,  
现在用输入的下标2 - sum(2) = 1 就可以得到对于2的逆序数为13. 输入1, 调用add(1, 1),把第1位设置为1  
1 2 3 4 5  
1 1 0 0 1  
计算1-1上比1小的数字存在么? 这里用到了树状数组的sum1) = 1操作,  
现在用输入的下标 3 -sum(1) = 2 就可以得到对于1的逆序数为24. 输入4, 调用add(4, 1),把第5位设置为1  
1 2 3 4 5  
1 1 0 1 1  
计算1-4上比4小的数字存在么? 这里用到了树状数组的sum4) = 3操作,  
现在用输入的下标4 - sum(4) = 1 就可以得到对于4的逆序数为15. 输入3, 调用add(3, 1),把第3位设置为1  
1 2 3 4 5  
1 1 1 1 1  
计算1-3上比3小的数字存在么? 这里用到了树状数组的sum3) = 3操作,  
现在用输入的下标5 - sum(3) = 2 就可以得到对于3的逆序数为26. 0+1+2+1+2 = 6 这就是最后的逆序数  
分析一下时间复杂度,首先用到快速排序,时间复杂度为O(NlogN),  
后面是循环插入每一个数字,每次插入一个数字,分别调用一次add()和sum()  
外循环N, add()和sum()时间O(logN) => 时间复杂度还是O(NlogN)  
#include <iostream>  
#include <cstdio>  
#include <algorithm>  
using  namespace std;  
typedef  long  long  ll;  
const int N=5e5+10;    
struct  node
{  
  int val;  
  int pos;  
}p[N];  
int n,bit[N],a[N];    
bool cmp(const node&a, const node& b)
{  
  return a.val<b.val;  
}   
void add(int i)
{  
  while(i<=n)
  {  
    bit[i]+=1;  
    i+=i&-i;  
  }  
}   
int sum(int i)
{  
  int s=0;  
  while(i>0)
  {  
    s+=bit[i];  
    i-=i&-i;  
  }  
  return s;  
}   
void solve()
{  
  for(int i=1; i<=n; i++)
  {  
    scanf("%d",&p[i].val);  
    p[i].pos=i;  
  }  
  sort(p+1,p+n+1,cmp);//排序  
  for(int i=1; i<=n; i++)
       a[p[i].pos]=i;//离散化  
  ll ans=0;  
  for (int i=1; i<=n; i++) 
      bit[i]=0;   //初始化树状数组  
  for(int i=1; i<=n; i++)
  {  
    add(a[i]);  
    ans+=i-sum(a[i]); //i为当前已经插入的数的个数.sum(a[i])为比a[i]小的数的个数 
  }  
  printf("%I64d\n",ans);  
}  
int main()
{  
  while(~scanf("%d",&n)&&n)
  {  
    solve();  
  }  
  return 0;  
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值