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

  1. 5
  2. 9
  3. 1
  4. 0
  5. 5
  6. 4
  7. 3
  8. 1
  9. 2
  10. 3
  11. 0

Sample Output

  1. 6
  2. 0

题意:

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

Solution:

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

代码:

  1. #include<bits/stdc++.h>
  2. #define il inline
  3. #define ll long long
  4. using namespace std;
  5. const int N=;
  6. int aa[N],c[N],n;
  7. struct node{
  8. int v,order;
  9. }a[N];
  10. il bool cmp(node a,node b){return a.v<b.v;}
  11. il void update(int t,int v)
  12. {
  13. while(t<=n){
  14. c[t]+=v;
  15. t+=(t&-t);
  16. }
  17. }
  18. il int getsum(int t)
  19. {
  20. int sum=;
  21. while(t){
  22. sum+=c[t];
  23. t-=(t&-t);
  24. }
  25. return sum;
  26. }
  27. il int gi()
  28. {
  29. int a=;char x=getchar();bool f=;
  30. while((x<''||x>'')&&x!='-')x=getchar();
  31. if(x=='-')x=getchar(),f=;
  32. while(x>=''&&x<='')a=a*+x-,x=getchar();
  33. return f?-a:a;
  34. }
  35. int main()
  36. {
  37. while(){
  38. n=gi();
  39. if(!n)return ;
  40. memset(c,,sizeof(c));
  41. for(int i=;i<=n;i++)a[i].v=gi(),a[i].order=i;
  42. sort(a+,a+n+,cmp);
  43. for(int i=;i<=n;i++)aa[a[i].order]=i;
  44. ll ans=;
  45. for(int i=n;i>;i--)update(aa[i],),ans+=getsum(aa[i]-);
  46. printf("%lld\n",ans);
  47. }
  48. }

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. day 4 继承

    1.继承引入,减少代码量 1)版本1: class Animal: '''定义一个动物类''' def eat(self): print("----吃----") def drin ...

  2. 【LG4148】简单题

    [LG4148]简单题 题面 洛谷 题解 \(kdt\)模板题呀... #include <iostream> #include <cstdio> #include <c ...

  3. 【转】 线段树完全版 ~by NotOnlySuccess

    载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...

  4. Zabbix学习之路(一)之Zabbix安装

    一.Zabbix环境准备 [root@linux-node1 ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@linux-n ...

  5. 算法工程师进化-NLP之主题模型

    1 引言 主题模型是文本挖掘的重要工具,近年来在学术界和工业届都获得了非常多的关注.学术界的工作主要集中在建模层面,即提出各种各样的主题模型来适应不同的场景,因此缺乏指导主题模型在工业场景落地的资源和 ...

  6. 建立 Python 虚拟环境

    1.1 安装依赖包 $ yum -y install wget gcc epel-release git 1.2 安装 Python3.6和pip $ yum -y install python36 ...

  7. [转载]文件系统缓存dirty_ratio与dirty_background_ra

    原文地址:文件系统缓存dirty_ratio与dirty_background_ratio两个参数区别作者:vincent 这两天在调优数据库性能的过程中需要降低操作系统文件Cache对数据库性能的影 ...

  8. Linux内核学习笔记(3)-- 进程的创建和终结

    一. 进程创建: Unix 下的进程创建很特别,与许多其他操作系统不同,它分两步操作来创建和执行进程: fork() 和 exec() .首先,fork() 通过拷贝当前进程创建一个子进程:然后,ex ...

  9. 从零开始的Python学习Episode 8——深浅拷贝

    深浅拷贝 一.浅拷贝 列表中存储的是数据的内存地址,当我们要查询或修改列表中的数据时,我们是通过列表中的地址找到要访问的内存.当我们修改列表中的数据时,如果修改的是一个不可变类型(整型,长整型,浮点数 ...

  10. Hackerank-Array-NewYearChaos

    题目背景描述 新年第一天,N 个人排队坐过山车.每个人穿有带编号的衣服 \([1, 2, 3, ...]\). 因为排队时间太久,有人发现给前面相邻的人喂一颗糖,就可以和他交换位置,而每人手里只有两颗 ...