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 ;
}

Poj 2299 - Ultra-QuickSort 离散化,树状数组,逆序对的更多相关文章

  1. [树状数组+逆序对][NOIP2013]火柴排队

    火柴排队 题目描述 涵涵有两盒火柴,每盒装有n根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:∑ (ai-bi)2,i=1,2,3,. ...

  2. Bzoj 3289: Mato的文件管理 莫队,树状数组,逆序对,离散化,分块

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 1539  Solved: 665[Submit][Status][Di ...

  3. HDU 2689Sort it 树状数组 逆序对

    Sort it Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  4. hdu 5497 Inversion 树状数组 逆序对,单点修改

    Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...

  5. Codevs 3286 火柴排队 2013年NOIP全国联赛提高组 树状数组,逆序对

    题目:http://codevs.cn/problem/3286/ 3286 火柴排队  2013年NOIP全国联赛提高组  时间限制: 1 s   空间限制: 128000 KB   题目等级 : ...

  6. Bzoj 2789: [Poi2012]Letters 树状数组,逆序对

    2789: [Poi2012]Letters Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 278  Solved: 185[Submit][Stat ...

  7. Bzoj 3295: [Cqoi2011]动态逆序对 分块,树状数组,逆序对

    3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2886  Solved: 924[Submit][Stat ...

  8. hdu 2838 Cow Sorting (树状数组+逆序对)

    题目 题意:给你N个排列不规则的数,任务是把它从小到大排好,每次只能交换相邻两个数,交换一次的代价为两数之和,求最小代价 拿到这道题,我根本看不出这道题和树状数组有半毛钱关系,博客之,全说用树状数组做 ...

  9. 【树状数组逆序对】USACO.2011JAN-Above the median

    [题意] 给出一串数字,问中位数大于等于X的连续子串有几个.(这里如果有偶数个数,定义为偏大的那一个而非中间取平均) [思路] 下面的数据规模也小于原题,所以要改成__int64才行.没找到测试数据, ...

随机推荐

  1. c++ fstream中seekg()和seekp()的用法

    转自:http://blog.sina.com.cn/s/blog_679f85d40100mysi.html 先说一下C语言中fseek()的功能: 函数原型:int fseek(FILE *fp, ...

  2. 安装grid之前检查配置 ,报错如下

    centos 5 _x86_64 oracle 11.2 安装grid之前检查配置 ,报错如下 : ./runcluvfy.sh stage -pre crsinst -n rac1,rac2 -fi ...

  3. 《C语言学习笔记》指针数组及其应用

    C语言中,最灵活但又容易出错的莫过于指针了.而指针数组,是在C中很常见的一个应用.指针数组的意思是说,这个数组存储的所有对象都为指针.除了存储对象为指针,即一个地址外,其它操作和普通数组完全一样. # ...

  4. express的基本配置项

    express自动生成的app.js中有一段代码用app.set和app.use对express进行配置,但这些配置都是什么意思,以及都能做哪些配置并没有展开.这一节就专门来讲express的配置.上 ...

  5. js 验证输入框金额

    $("#ipt1").keyup(function () { var reg = $(this).val().match(/\d+\.?\d{0,2}/); var txt = ' ...

  6. PHP 冒泡排序法

    <?php // 冒泡排序法:将一个数组中的值按照从小到大的顺 序排序 $arr = array(33, 1, 4, 5, 2, 3, 7, 9, 8, 99); $len = count($a ...

  7. TDirectory.GetFiles获取指定目录下的文件

    使用函数: System.IOUtils.TDirectory.GetFiles 所有重载: class function GetFiles(const Path: string): TStringD ...

  8. poco异步等待ActiveResult

    #include "Poco/ActiveMethod.h"#include "Poco/ActiveResult.h"#include <utility ...

  9. python学习--string

    1\string are immutable, which means you can't change an existing string. >>>greeting = 'Hel ...

  10. Log4j与common-logging

    Log4j与common-logging 总网上搜了些Log4j与common-logging的介绍,记录下. 一.Log4j  1.简介 Log4j是Apache的一个开放源代码项目 使用Log4j ...