poj2299——Ultra-QuickSort
Description
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
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
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的更多相关文章
- quickSort算法导论版实现
本文主要实践一下算法导论上的快排算法,活动活动. 伪代码图来源于 http://www.cnblogs.com/dongkuo/p/4827281.html // imp the quicksort ...
- Javascript算法系列之快速排序(Quicksort)
原文出自: http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/ https://gis ...
- JavaScript 快速排序(Quicksort)
"快速排序"的思想很简单,整个排序过程只需要三步: (1)在数据集之中,选择一个元素作为"基准"(pivot). (2)所有小于"基准"的元 ...
- QuickSort 快速排序 基于伪代码实现
本文原创,转载请注明地址 http://www.cnblogs.com/baokang/p/4737492.html 伪代码 quicksort(A, lo, hi) if lo < hi p ...
- quicksort
快排.... void quicksort(int *a,int left,int right){ if(left >= right){ return ; } int i = left; int ...
- 随手编程---快速排序(QuickSort)-Java实现
背景 快速排序,是在上世纪60年代,由美国人东尼·霍尔提出的一种排序方法.这种排序方式,在当时已经是非常快的一种排序了.因此在命名上,才将之称为"快速排序".这个算法是二十世纪的七 ...
- Teleport Ultra 下载网页修复
1 三个基本正则替换 tppabs="h[^"]*"/\*tpa=h[^"]*/javascript:if\(confirm\('h[^"]*[Ult ...
- Ultra Video Splitter & Converter
1. Video Splitter http://www.aone-soft.com/splitter.htm Ultra Video Splitter 是一款视频分割工具.可将一个巨大的AVI/Di ...
- 算法实例-C#-快速排序-QuickSort
算法实例 ##排序算法Sort## ### 快速排序QuickSort ### bing搜索结果 http://www.bing.com/knows/search?q=%E5%BF%AB%E9%80% ...
- mac 激活Ultra Edit16
一.文本编辑器UltraEdit 参照Ultra Edit16.10 Mac 破解下载,或者官方下载 Ultra Edit16即可 printf of=/Applications/UltraEdit. ...
随机推荐
- day 4 继承
1.继承引入,减少代码量 1)版本1: class Animal: '''定义一个动物类''' def eat(self): print("----吃----") def drin ...
- 【LG4148】简单题
[LG4148]简单题 题面 洛谷 题解 \(kdt\)模板题呀... #include <iostream> #include <cstdio> #include <c ...
- 【转】 线段树完全版 ~by NotOnlySuccess
载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...
- Zabbix学习之路(一)之Zabbix安装
一.Zabbix环境准备 [root@linux-node1 ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@linux-n ...
- 算法工程师进化-NLP之主题模型
1 引言 主题模型是文本挖掘的重要工具,近年来在学术界和工业届都获得了非常多的关注.学术界的工作主要集中在建模层面,即提出各种各样的主题模型来适应不同的场景,因此缺乏指导主题模型在工业场景落地的资源和 ...
- 建立 Python 虚拟环境
1.1 安装依赖包 $ yum -y install wget gcc epel-release git 1.2 安装 Python3.6和pip $ yum -y install python36 ...
- [转载]文件系统缓存dirty_ratio与dirty_background_ra
原文地址:文件系统缓存dirty_ratio与dirty_background_ratio两个参数区别作者:vincent 这两天在调优数据库性能的过程中需要降低操作系统文件Cache对数据库性能的影 ...
- Linux内核学习笔记(3)-- 进程的创建和终结
一. 进程创建: Unix 下的进程创建很特别,与许多其他操作系统不同,它分两步操作来创建和执行进程: fork() 和 exec() .首先,fork() 通过拷贝当前进程创建一个子进程:然后,ex ...
- 从零开始的Python学习Episode 8——深浅拷贝
深浅拷贝 一.浅拷贝 列表中存储的是数据的内存地址,当我们要查询或修改列表中的数据时,我们是通过列表中的地址找到要访问的内存.当我们修改列表中的数据时,如果修改的是一个不可变类型(整型,长整型,浮点数 ...
- Hackerank-Array-NewYearChaos
题目背景描述 新年第一天,N 个人排队坐过山车.每个人穿有带编号的衣服 \([1, 2, 3, ...]\). 因为排队时间太久,有人发现给前面相邻的人喂一颗糖,就可以和他交换位置,而每人手里只有两颗 ...