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

题目地址

题目大意

给定一个序列求逆序数,用树状数组解决,注意数据太大需离散化处理

AC代码

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
using namespace std;
const int M=500010;
int MN;
int e[M];
int k;
struct node
{
int val;
int pos;
}a[M];
int lowbit(int x)
{
return x&(-x);
}
void update(int i,int v)
{
for(;i<=MN;i+=lowbit(i)){
e[i]+=v;
}
}
int getsum(int i)
{
int res=0;
for(;i>0;i-=lowbit(i)){
res+=e[i];
}
return res;
}
bool cmp1(node a,node b)
{
return a.val<b.val;
}
bool cmp2(node a,node b)
{
return a.pos<b.pos;
}
void init()
{
sort(a+1,a+k+1,cmp1);
for(int i=1;i<=k;i++)
a[i].val=i;
sort(a+1,a+k+1,cmp2);
memset(e,0,sizeof(e));
MN=k;
}
int main()
{
//freopen("data.in","r",stdin);
long long res;
while(cin>>k&&k!=0){
res=0;
for(int i=1;i<=k;i++){
cin>>a[i].val;
a[i].pos=i;
}
init();
for(int i=1;i<=k;i++){
update(a[i].val,1);
res+=(i-getsum(a[i].val));
}
cout<<res<<endl;
}
}

POJ2299--树状数组求逆序数的更多相关文章

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

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

  2. hdu 5147 Sequence II (树状数组 求逆序数)

    题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. SGU180 Inversions(树状数组求逆序数)

    题目: 思路:先离散化数据然后树状数组搞一下求逆序数. 离散化的方法:https://blog.csdn.net/gokou_ruri/article/details/7723378 自己对用树状数组 ...

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

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

  5. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  6. Codeforces645B【树状数组求逆序数】

    题意: 给你1-n的序列,然后有k次机会的操作,每一次你可以选择两个数交换. 求一个最大的逆序数. 思路: 感觉就是最后一个和第一个交换,然后往中间逼近,到最终的序列,用树状数组求一下逆序数. #in ...

  7. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  8. hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数 && 归并排序求逆序数)

    题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m ...

  9. Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem (树状数组求逆序数 变形)

    题目链接 题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求i和j的种类数. 我们可以用map预处理出  ...

  10. poj 2229 Ultra-QuickSort(树状数组求逆序数)

    题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...

随机推荐

  1. [ An Ac a Day ^_^ ] UVALive 7270 Osu! Master

    2015icpc北京区域赛的签到题 还是很签到的 一次就过了 题意呢 就是统计B C后最长上升序列还有S的个数 当然B C要被S分割开…… /* *************************** ...

  2. 不完善的css怦然心动,有待改进...

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  3. UNIX基础--权限

    权限 Permissions FreeBSD使用传统的UNIX®系统的基本权限.在UNIX®系统中,基本权限分配了三种访问类型:读.写.执行.权限可以用字母r.w.x表示:也可以用二进制数表示,按rw ...

  4. vc中主线程等待子线程退出的方法

    VC线程同步,在子线程中等待另一子线程结束,通过WaitForSingleObject可以实现,但是如果在主线程中等待子线程结束,这个函数是无法完成要求的,因为它会造成主线程挂起,导致程序死掉.我们可 ...

  5. java反射机制(1)

    反射,当时经常听他们说,自己也看过一些资料,也可能在设计模式中使用过,但是感觉对它没有一个较深入的了解,这次重新学习了一下,感觉还行吧! 一,先看一下反射的概念: 主要是指程序可以访问,检测和修改它本 ...

  6. jsp页面中EL表达式不能被解析

    原因是:在默认情况下,Servlet 2.4 / JSP 2.0支持 EL 表达式. 用maven插件的生成的webApp的项目结构比较老的是2.3的版本,只要将web中的开头定义换成2.4以上的定义 ...

  7. 【ADT】链表的基本C语言实现

    什么是抽象数据类型?首先,这一概念是软件开发人员在力求编写的代码健壮.易维护且可以复用的过程中产生的.英文是AbstractData Type.有人将其比作"抽象"的墙壁,&quo ...

  8. 将数据动态加载到Echarts饼图中

    需求描述 Echarts中的官方示例是将数据的设定写好在页面的配置项中的,但在实际的开发展示中,我们需要按照需求通过调用后台的接口获取数据,再将数据加载到特定的Echarts饼图中. 实现效果 实现步 ...

  9. c/s架构

    C/S 结构,即大家熟知的客户机和服务器结构.它是软件系统体系结构,通过它可以充分利用两端硬件环境的优势,将任务合理分配到Client端和Server端来实现,降低了系统的通讯开销.目前大多数应用软件 ...

  10. html5 --基础笔记2

    1.autocomplete 可以给表单本身(不是fieldset)设置属性来禁用整个表单的自动完成功能 <form id="" method="" au ...