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

题意:

给定一个长度为n(n<=500000)的序列a,如果只允许进行比较和交换相邻两个数的操作,求至少需要多少次交换才能把a从小到大排序。

Solution:

容易发现每次交换和比较相邻的两个数使序列有序,就是冒泡排序,而冒泡排序的最少操作数就是序列中的逆序数对的个数,所以本题转换为求序列中的逆序对的个数。于是就可以用树状数组求啦。由于本题数据较大,所以我们需要对数值离散化,然后排序,按倒序update并getsum求在它更新之前有多少个数在其前面,详细见我的树状数组详解的博客。

代码:

#include<bits/stdc++.h>
#define il inline
#define ll long long
using namespace std;
const int N=;
int aa[N],c[N],n;
struct node{
int v,order;
}a[N];
il bool cmp(node a,node b){return a.v<b.v;}
il void update(int t,int v)
{
while(t<=n){
c[t]+=v;
t+=(t&-t);
}
}
il int getsum(int t)
{
int sum=;
while(t){
sum+=c[t];
t-=(t&-t);
}
return sum;
}
il int gi()
{
int a=;char x=getchar();bool f=;
while((x<''||x>'')&&x!='-')x=getchar();
if(x=='-')x=getchar(),f=;
while(x>=''&&x<='')a=a*+x-,x=getchar();
return f?-a:a;
}
int main()
{
while(){
n=gi();
if(!n)return ;
memset(c,,sizeof(c));
for(int i=;i<=n;i++)a[i].v=gi(),a[i].order=i;
sort(a+,a+n+,cmp);
for(int i=;i<=n;i++)aa[a[i].order]=i;
ll ans=;
for(int i=n;i>;i--)update(aa[i],),ans+=getsum(aa[i]-);
printf("%lld\n",ans);
}
}

poj2299——Ultra-QuickSort的更多相关文章

  1. quickSort算法导论版实现

    本文主要实践一下算法导论上的快排算法,活动活动. 伪代码图来源于 http://www.cnblogs.com/dongkuo/p/4827281.html // imp the quicksort ...

  2. Javascript算法系列之快速排序(Quicksort)

    原文出自: http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/ https://gis ...

  3. JavaScript 快速排序(Quicksort)

    "快速排序"的思想很简单,整个排序过程只需要三步: (1)在数据集之中,选择一个元素作为"基准"(pivot). (2)所有小于"基准"的元 ...

  4. QuickSort 快速排序 基于伪代码实现

    本文原创,转载请注明地址 http://www.cnblogs.com/baokang/p/4737492.html 伪代码 quicksort(A, lo, hi) if lo < hi p ...

  5. quicksort

    快排.... void quicksort(int *a,int left,int right){ if(left >= right){ return ; } int i = left; int ...

  6. 随手编程---快速排序(QuickSort)-Java实现

    背景 快速排序,是在上世纪60年代,由美国人东尼·霍尔提出的一种排序方法.这种排序方式,在当时已经是非常快的一种排序了.因此在命名上,才将之称为"快速排序".这个算法是二十世纪的七 ...

  7. Teleport Ultra 下载网页修复

    1 三个基本正则替换 tppabs="h[^"]*"/\*tpa=h[^"]*/javascript:if\(confirm\('h[^"]*[Ult ...

  8. Ultra Video Splitter & Converter

    1. Video Splitter http://www.aone-soft.com/splitter.htm Ultra Video Splitter 是一款视频分割工具.可将一个巨大的AVI/Di ...

  9. 算法实例-C#-快速排序-QuickSort

    算法实例 ##排序算法Sort## ### 快速排序QuickSort ### bing搜索结果 http://www.bing.com/knows/search?q=%E5%BF%AB%E9%80% ...

  10. mac 激活Ultra Edit16

    一.文本编辑器UltraEdit 参照Ultra Edit16.10 Mac 破解下载,或者官方下载 Ultra Edit16即可 printf of=/Applications/UltraEdit. ...

随机推荐

  1. 解决非controller使用,@Autowired或者@Resource注解注入Mapper接口为null的问题

    知识点:在service层中注入其它的service接口或者mapper接口都是可以的 但是在封装的Utils工具类中或者非controller普通类中使用@Autowired@Resource注解注 ...

  2. git拉代码,IntelliJ idea报错,cannot load module xxxxx

    1 从git上下工程的时候,IntelliJ idea报错,cannot load module xxxx VCS-git-clone-ssh:xxxx ,报错cannot load module x ...

  3. jenkins升级为2.134

    由于前面装的jenkins版本为2.130版本,昨天(2018.7.26)发现了两个jenkins的漏洞,影响范围为:Jenkins weekly 2.132 以及更早的版本.Jenkins LTS ...

  4. 阿里云服务器Centos上Apache安装SSL证书配置Https

    首先我们先去阿里云申请一个免费的SSL证书(https://common-buy.aliyun.com/?spm=5176.7968328.1266638..5e971232BzMSp5&co ...

  5. openstack系列文章(三)

    学习openstack的系列文章-glance glance 基本概念 glance 架构 openstack CLI Troubleshooting 1. glance 基本概念 在 opensta ...

  6. php-fpm配置

    [global] error_log = /letv/log/php-fpm_error.log [www] user = apache group = apache listen = 127.0.0 ...

  7. USACO 1.3.4 Prime Cryptarithm 牛式(模拟枚举)

    Description 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. * * * x * * ------- * * * * * * ------ ...

  8. Thunder团队第七周 - Scrum会议5

    Scrum会议5 小组名称:Thunder 项目名称:i阅app Scrum Master:邹双黛 工作照片: 宋雨沉迷于照相无法自拔,所以不在相片中. 参会成员: 王航:http://www.cnb ...

  9. c# richBox内容转图片

    1.自定义控件,继承richBox public class RichTextBoxPrintCtrl : RichTextBox { //private const double anInch = ...

  10. 四则运算(Android)版

    实验题目: 将小学四则运算整合成网页版或者是Android版.实现有无余数,减法有无负数.... 设计思路: 由于学到的基础知识不足,只能设计简单的加减乘除,界面设计简单,代码量少,只是达到了入门级的 ...