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 给你一个排序,让你求出这个排序中的逆序数.算是自己写的树状数组的第一道题吧,这个题首先用到离散化思想.
因为每个数都是小于1e9,而n的范围不超过5e5,而且我们只关心这些数的大小关系,所以我们就把这些数全部映射到1~n,然后再按照排好序的位置插进去就行了
这样子是不会影响最后算逆序数的.映射后的序列为reflect
接下来求逆序数,这时要用到树状数组,树状数组的功能是用来求数组前缀和的,我们假想有一个长度为n的数组,它现在每一位初始化为0
我们按照位置顺序将reflect中的每一个数插入到树状数组中,假设我们现在插入的数是第i个数,它的值为x
那么我就把刚刚假想的数组的第x位置变为1,即代表x被插入到该序列
那么每一次对于ans怎样+呢?
我们可以定义sum(x) x及小于x的数中有多少个元素已经被插入
这样的话sum(x)就是我们刚刚那个假想数组的前缀和了,我们用树状数组可以求
对于插入的第i个数,我们的ans+=i-sum(x),因为x是第i个数插进来的,之前插进来小于等于x的数为sum(x)
两者做差就是在x之前插入,而且值要大于x的数字的个数
代码如下:
 #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn =;
int reflect[maxn];
int c[maxn];
int n;
int lowbit (int x)
{
return x&(-x);
}
struct Node
{
int val,pos;
};
Node node[maxn];
bool cmp (Node q1,Node q2)
{
return q1.val<q2.val;
} void update (int x)
{
while (x<=n){
c[x]+=;//将我们想象的数组x位置附为1,直接就加到c[x]上了
x+=lowbit(x);//找父节点
}
}
int getsum (int x)
{
int sum=;
while (x>){
sum+=c[x];
/*求和时可以看作将x用二进制表示
temp=x;
每次加上c[temp],再把temp最后1位置上的值改成0
最后变成0时就求完了
如:求 101001
temp=101001 -> sum+=c[101001]
-> temp=101000 -> sum+=c[101000]
-> temp=100000 -> sum+=c[100000]
-> temp=000000 求完了
*/
x-=lowbit(x);
}
return sum;
}
int main()
{
//freopen("de.txt","r",stdin);
while (scanf("%d",&n)&&n){
for (int i=;i<=n;++i){
scanf("%d",&node[i].val);
node[i].pos=i;
}
sort(node+,node + n + , cmp);
for (int i=;i<=n;++i) reflect[node[i].pos]=i;//离散化映射到1~n
for (int i=;i<=n;++i) c[i]=;//初始化树状数组
long long ans=;
for (int i=;i<=n;++i){
update(reflect[i]);//插入一个数,更新一下
ans+=i-getsum(reflect[i]);//ans加一下
}
printf("%lld\n",ans);
}
return ;
}
 

POJ 2299 Ultra-QuickSort (树状数组+离散化 求逆序数)的更多相关文章

  1. HDU 1394 树状数组+离散化求逆序数

    对于求逆序数问题,学会去利用树状数组进行转换求解方式,是很必要的. 一般来说我们求解逆序数,是在给定一串序列里,用循环的方式找到每一个数之前有多少个比它大的数,算法的时间复杂度为o(n2). 那么我们 ...

  2. CF 61E 树状数组+离散化 求逆序数加强版 三个数逆序

    http://codeforces.com/problemset/problem/61/E 题意是求 i<j<k && a[i]>a[j]>a[k] 的对数 会 ...

  3. POJ 2299 Ultra-QuickSort(树状数组+离散化)

    http://poj.org/problem?id=2299 题意:给出一组数,求逆序对. 思路: 这道题可以用树状数组解决,但是在此之前,需要对数据进行一下预处理. 这道题目的数据可以大到999,9 ...

  4. POJ - 2299 Ultra-QuickSort 【树状数组+离散化】

    题目链接 http://poj.org/problem?id=2299 题意 给出一个序列 求出 这个序列要排成有序序列 至少要经过多少次交换 思路 求逆序对的过程 但是因为数据范围比较大 到 999 ...

  5. POJ 2299 树状数组+离散化求逆序对

    给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...

  6. POJ 3067 - Japan - [归并排序/树状数组(BIT)求逆序对]

    Time Limit: 1000MS Memory Limit: 65536K Description Japan plans to welcome the ACM ICPC World Finals ...

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

    题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...

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

    链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...

  9. poj 2299 Ultra-QuickSort(树状数组)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 67681   Accepted: 25345 ...

随机推荐

  1. Servlet 第一天

    package com.servlet; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet. ...

  2. ES6中类Class的super关键字

    super 关键字,既可以当作函数使用,也可以当作对象使用.在这两种情况下,它的用法完全不同. 1.super当做函数使用 super 作为函数调用时,代表父类的构造函数.ES6 要求,子类的构造函数 ...

  3. paper 153:Delaunay三角剖分算法--get 这个小技术吧!

    直接摘自百度百科,希望大家能根据下面的介绍稍微理顺思路,按需使用,加油! 解释一下:点集的三角剖分(Triangulation),对数值分析(比如有限元分析)以及图形学来说,都是极为重要的一项预处理技 ...

  4. SpringBoot 快速构建微服务体系 知识点总结

    可以通过http://start.spring.io/构建一个SpringBoot的脚手架项目 一.微服务 1.SpringBoot是一个可使用Java构建微服务的微框架. 2.微服务就是要倡导大家尽 ...

  5. AtCoder Grand Contest 012 A - AtCoder Group Contest(贪心)

    Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement There are 3N participa ...

  6. 左手Mongodb右手Redis redis操作

    set key value  设置key的值 get key 取得key的值 decr key 值会减一 incr key 值会加一 decrby key value ,会让key的值减少value. ...

  7. Dubbo 系列(07-3)集群容错 - 负载均衡

    目录 Dubbo 系列(07-3)集群容错 - 负载均衡 Spring Cloud Alibaba 系列目录 - Dubbo 篇 1. 背景介绍 1.1 负载均衡算法 1.2 继承体系 2. 源码分析 ...

  8. PHP pthread多线程

    class test extends Thread { public $arg; public function __construct($arg){ $this->arg = $arg; } ...

  9. Ubuntu下配置了ssh,但是连接很慢

    ssh登录服务器时总是要停顿等待一下才能连接上,这是因为OpenSSH服务器有一个DNS查找选项UseDNS默认是打开的. UseDNS选项打开状态下,当客户端试图登录OpenSSH服务器时,服务器端 ...

  10. Spring学习笔记(6)——IoC的三种注入方式

    1.接口注入(不推荐) 2.构造器注入(死的应用) 3.getter,setter方式注入(比较常用) Type1 接口注入 我们常常借助接口来将调用者与实现者分离.如: public class C ...