Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 52306   Accepted: 19194

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

Source

 
题意:给定n个数,只能交换相邻的两个元素,至少交换几次,成为递增序列。
题解:
明显要求序列的逆序对数目。。。
对于样例:
5
9 1 0 5 4
我们将其排序:
0 1 4 5 9
在每个位置上初始放为1.
1 1 1 1 1
然后,从原序列开始遍历。
先到9,我们把其排好序的位置拿出,即为5。
然后统计位置5之前有多少1。
1 1 1 1 1
————  > 4个  ans+=4
然后把5号位置放为0。
1 1 1 1 0
继续操作即可。。。
这个树状数组维护即可。。。
注意开long long和原序列排序后要去重。。。
 #include<bits/stdc++.h>
using namespace std;
#define MAXN 500010
#define LL long long
int n,BIT[MAXN],a[MAXN],aa[MAXN];
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
int Lowbit(int o){return o&(-o);}
void Update(int o,int o1)
{
while(o<=n)
{
BIT[o]+=o1;
o+=Lowbit(o);
}
}
int Sum(int o)
{
int sum=;
while(o>)
{
sum+=BIT[o];
o-=Lowbit(o);
}
return sum;
}
int main()
{
int tot,i,wz;
LL ans;
while()
{
n=read();if(n==)break;
for(i=;i<=n;i++){a[i]=read();aa[i]=a[i];}
memset(BIT,,sizeof(BIT));
sort(a+,a+n+);
tot=unique(a+,a+n+)-(a+);
for(i=;i<=tot;i++)Update(i,);
ans=;
for(i=;i<=n;i++)
{
wz=lower_bound(a+,a+tot+,aa[i])-a;
ans+=(LL)Sum(wz-);
if(BIT[wz]!=)Update(wz,-);
}
printf("%lld\n",ans);
}
fclose(stdin);
fclose(stdout);
return ;
}

最新文章

  1. 【转】PowerShell入门(十二):编写PowerShell管理单元和二进制模块
  2. 利用SoapUI 测试web service的方法介绍
  3. 我的c++学习(7)引用和复制构造函数
  4. 使用redis的五个注意事项
  5. hibernate多表查询,结果封装在自己定义的一个实体类当中(在自己定义的类中增加构造函数)
  6. word 使用宏批量设置表格
  7. Knockout.js, Asp.Net MVC and Bootstrap 前端设计
  8. 概述什么是OSGi框架
  9. Tkinter教程之Text(2)篇
  10. hdu 1195 广度搜索
  11. hdoj 5443 The Water Problem【线段树求区间最大值】
  12. Browsing History
  13. API 之 MessageBox
  14. linux系统文件属性-硬连接、软连接
  15. redis client protocol 分解
  16. Linux下jdk环境配置
  17. 最基础的 swift 语言
  18. DLC 基本定律与规则2
  19. Codefoces 277 E. Binary Tree on Plane
  20. REST easy with kbmMW #24 使用kbmMW实现JSON/XML/YAML转换成对象

热门文章

  1. SQLite学习第02天:数据类型
  2. $.ajax参数备注-转转转
  3. Gitlab服务器搭建(For fedora23)
  4. div 被Object盖住的。解决办法
  5. 测试php页面执行代码时间
  6. VS2013发布web项目到IIS上遇到的问题总结
  7. [swift]可选类型
  8. 在虚拟机的linux中利用VMware Tools实现与windows共享文件
  9. 一些Swift编程语言的相关资料
  10. nump中的为随机数产生器的seed