Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 40827   Accepted: 14752

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

求冒泡排序交换的次数。
因为这些数可能太大。且差距非常大,所以离散化一下,然后求一下逆序数。边查询边插入边就可以。


//32684K	1579MS
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define M 500007
#define ll __int64
using namespace std;
int s[M],n;
struct Tree
{
int l,r,mid;
ll val;
}tree[M<<1];
struct sa
{
int id;
ll val;
}p[M*2];
int cmp(sa a,sa b)
{
return a.val>b.val;
}
void build(int left,int right,int i)
{
tree[i].l=left;tree[i].r=right;tree[i].mid=(left+right)>>1;tree[i].val=0;
if(left==right){return;}
build(left,tree[i].mid,i*2);
build(tree[i].mid+1,right,i*2+1);
}
int query(int x,int i)
{
if(tree[i].l==tree[i].r)return tree[i].val;
if(x<=tree[i].mid)return query(x,i*2)+tree[i].val;
else return query(x,i*2+1)+tree[i].val;
}
void insert(int left,int right,int i)
{
if(tree[i].l==left&&tree[i].r==right){tree[i].val++;return;}
if(right<=tree[i].mid)insert(left,right,2*i);
else if(left>tree[i].mid)insert(left,right,2*i+1);
else {insert(left,tree[i].mid,i*2);insert(tree[i].mid+1,right,i*2+1);}
}
void discretization()
{
int tmp=p[1].val,pos=1;
for(int i=1;i<=n;i++)
if(p[i].val!=tmp)p[i].val=++pos,tmp=p[i].val;
else p[i].val=pos;
for(int i=1;i<=n;i++)
s[p[i].id]=p[i].val;
}
int main()
{
while(scanf("%d",&n)&&n)
{
ll ans=0;
build(0,M,1);
memset(s,0,sizeof(s));
for(int i=1;i<=n;i++)
{
scanf("%I64d",&p[i].val);
p[i].id=i;
}
sort(p+1,p+n+1,cmp);
discretization();
for(int i=1;i<=n;i++)
printf("%d ",s[i]);
printf("\n");
for(int i=1;i<=n;i++)
{
ans+=query(s[i],1);
insert(s[i],M,1);
}
printf("%I64d\n",ans);
}
return 0;
}

最新文章

  1. 介绍一款原创的四则运算算式生成器:CalculateIt2
  2. mango-1.4.1 文档
  3. Inno Setup 安装前卸载原程序
  4. Hive基础之Hive开启查询列名及行转列显示
  5. 使用proguard混淆android代码
  6. 如何让sudo命令不需要输入密码就可执行
  7. Learning Cocos2d-x for WP8(6)——场景切换和场景过渡效果
  8. POJ 1026 Cipher(更换)
  9. 安卓Service完全解析(中)
  10. javascript设计模式——适配器模式
  11. 驰骋工作流引擎JFlow与activiti的对比之4种包含多实例的模式
  12. vue-electron脚手架安装及说明 打包基于Vue的 桌面应用程序
  13. ios下,&lt;input type=&quot;checkbox&quot;&gt; 点击时出现黑色块
  14. P9架构师讲解从单机至亿级流量大型网站系统架构的演进过程
  15. 团队作业记账本开发NABCD
  16. 【亲测显式等待】Selenium:元素等待的4种方法
  17. ServiceMesh了解一下
  18. MySQL 的三个浮点类型
  19. 用Python免费发短信,实现程序实时报警
  20. bootstrap datatable

热门文章

  1. Javascript万物皆对象?
  2. insert into 语句的三种写法 以及批量插入
  3. mysql的递归(使用函数)
  4. Svn install and use
  5. js编写时间选择框
  6. Codeforces Round #453
  7. js中国各大城市快速选择代码
  8. 动画库animate.css的用法
  9. 【Oracle】创建角色
  10. 获取 Windows Phone 的 User-Agent 字符串