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

 
 
正解一:归并排序
解题报告:
  大概题意是求数列的冒泡排序排序次数,求逆序对的模板题。
  直接归并排序的时候统计一下就可以了。
  归并排序的提交记录:
15602638 ljh2000 2299 Accepted 4220K 188MS G++ 1401B 2016-06-10 15:08:59
 
 
 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#ifdef WIN32
#define OT "%I64d"
#else
#define OT "%lld"
#endif
using namespace std;
typedef long long LL;
const int MAXN = ;
int n,m;
int jump[MAXN];
int g[MAXN];
LL ans; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline void merge(int l,int mid,int r){
int i=l,j=mid+;
int cnt=l;
while(i<=mid && j<=r) {
if(jump[i]<=jump[j]) g[cnt++]=jump[i++];
else{
g[cnt++]=jump[j++];
//ans+=mid-i+1;
ans+=(LL)mid; ans-=(LL)i; ans++;
}
}
while(i<=mid) g[cnt++]=jump[i++];
while(j<=r) g[cnt++]=jump[j++];
//for(;i<=mid;i++) g[cnt++]=a[i];
//for(;j<=r;j++) g[cnt++]=a[i];
for(i=l;i<=r;i++) jump[i]=g[i];
} inline void gui(int l,int r){
if(l==r) return ;
int mid=(l+r)/;
gui(l,mid); gui(mid+,r);
merge(l,mid,r);
} inline void solve(){
while() {
n=getint();
if(n==) break;
for(int i=;i<=n;i++) jump[i]=getint();
ans=;
gui(,n);
printf(OT"\n",ans);
}
} int main()
{
solve();
return ;
}

正解二:树状数组

解题报告:

  树状数组也是可做的。

  由于数字比较大,先离散化一下,然后按顺序插入,插入之后看看已经有多少个数比自己大了,统计一下就可以了。

  比归并排序慢好多哦。。。

Run ID User Problem Result Memory Time Language Code Length Submit Time
15602746 ljh2000 2299 Accepted 7932K 532MS G++ 1256B 2016-06-10 15:41:43
 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#ifdef WIN32
#define OT "%I64d"
#else
#define OT "%lld"
#endif
using namespace std;
typedef long long LL;
const int MAXN = ;
int n,L;
LL ans;
int a[MAXN],u[MAXN];
int shu[MAXN],rank[MAXN]; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline void update(int x,int val){
while(x<=L) {
shu[x]+=val;
x+=x&(-x);
}
} inline LL query(int x){
LL total=;
while(x>) {
total+=shu[x];
x-=x&(-x);
}
return total;
} inline void solve(){
while() {
n=getint();
if(n==) break;
for(int i=;i<=n;i++) u[i]=a[i]=getint();
ans=;
memset(shu,,sizeof(shu));
sort(u+,u+n+);
L=unique(u+,u+n+)-u-;
for(int i=;i<=n;i++) {
rank[i]=lower_bound(u+,u+L+,a[i])-u;
update(rank[i],);
ans+=query(L)-query(rank[i]);
} printf(OT"\n",ans);
}
} int main()
{
solve();
return ;
}

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. java10-1 Object类

    Object:类      Object 是类层次结构的根类.每个类都使用 Object 作为超类. 每个类都直接或者间接的继承自Object类. Object类的方法: public int has ...

  2. Linux下squid代理缓存服务环境部署

    代理服务器英文全称是Proxy Server,其功能就是代理网络用户去取得网络信息. Squid是一个缓存Internet 数据的软件,其接收用户的下载申请,并自动处理所下载的数据.当一个用户想要下载 ...

  3. Power Builder的学习

    新的任务可能要运用PowerBuilder了,对这个名词之前仅是有所耳闻,工作中倒是用过power designer这个优秀的建模工具,出自同一家公司的产品,应该拥有同样的基因,于是上网开始查阅相关资 ...

  4. NET Office 组件Spire

    高效而稳定的企业级.NET Office 组件Spire   在项目开发中,尤其是企业的业务系统中,对文档的操作是非常多的,有时几乎给人一种错觉的是"这个系统似乎就是专门操作文档的" ...

  5. Linux 进程通信(无名管道)

    无名管道 无名管道是半双工的,就是对于一个管道来讲,只能读,或者写. 无名管道只能在相关的,有共同祖先的进程间使用(即一般用户父子进程). 一个fork或者execve调用创建的子进程继承了父进程的文 ...

  6. linux下c++开发环境安装(eclipse+cdt)

    方法一: 此外,众所周知,Eclipse是Java程序,因此很容易就实现了跨平台,也是众所周知,Java的大型程序非常吃内存,即使有512MB内存, 仍然感觉Eclipse的启动速度很慢.个人认为1G ...

  7. 怎么样快速学习AngularJS?

    其实AngularJS的官方网站首页的几个例子已经很好的展示了AngularJS的一些特性,下面我就从几个例子一步一步的讲解AngularJS吸引人的东西并且实际项目中是怎么使用ng的. 首先还是从第 ...

  8. TCP之心跳包实现思路

    说起网络应用编程,想到最多的就是聊天类的软件.当然,在这类软件中,一般都会有一个用户掉线检测功能.今天我们就通过使用自定义的HeartBeat方式来检测用户的掉线情况. 心跳包实现思路 我们采用的思路 ...

  9. Qt写入txt文件方法

    void MainWindow::on_saveBtn_clicked() { //本函数只是单独测试Qt保持为txt文本功能,与本串口程序无任何关系 QDateTime da_time; QStri ...

  10. C语言学习的记忆

    优于他人的技能 会玩双截棍: 我的经验就是Practice make perfect,熟能生巧:还有就是坚持不懈. 关于C语言的学习的回忆 1.我通过老师的教导和课外C语言书籍中学习,和我的技能相比, ...