Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 49267   Accepted: 18035

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
题解,错了半天,while出错,改成for循环就对了,至今不知道为啥。。。
代码:
 #include<stdio.h>
const int MAXN=;
int a[MAXN],b[MAXN];
long long ans;
void mergesort(int start,int mid,int end){
int i=start,k=start,j=mid+;
while(i<=mid&&j<=end){
if(a[i]<=a[j])b[k++]=a[i++];
else{
ans+=j-k;
b[k++]=a[j++];
}
}
while(i<=mid)b[k++]=a[i++];
while(j<=end)b[k++]=a[j++];
//while(start<=end)a[start++]=b[start];
for(int i=start;i<=end;i++)a[i]=b[i];
}
void ms(int l,int r){
if(l<r){
int mid=(l+r)/;
ms(l,mid);
ms(mid+,r);
mergesort(l,mid,r);
}
}
int main(){
int n;
while(scanf("%d",&n),n){
ans=;
for(int i=;i<=n;i++)scanf("%d",a+i);
ms(,n);
printf("%lld\n",ans);
}
return ;
}
												

Ultra-QuickSort(归并排序)的更多相关文章

  1. 归并排序(MergeSort)和快速排序(QuickSort)的一些总结问题

    归并排序(MergeSort)和快速排序(QuickSort)都是用了分治算法思想. 所谓分治算法,顾名思义,就是分而治之,就是将原问题分割成同等结构的子问题,之后将子问题逐一解决后,原问题也就得到了 ...

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

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

  3. ACM/ICPC 之 快排+归并排序-记录顺序对(TSH OJ-LightHouse(灯塔))

    TsingHua OJ 上不能使用<algorithm>头文件,因此需要手写快排(刚开始写的时候自己就出了很多问题....),另外本题需要在给横坐标排序后,需要记录纵坐标的顺序对的数量,因 ...

  4. Ultra-QuickSort【归并排序典型题目】

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 34470   Accepted: 12382 ...

  5. leetcode:Sort List(一个链表的归并排序)

    Sort a linked list in O(n log n) time using constant space complexity. 分析:题目要求时间复杂度为O(nlogn),所以不能用qu ...

  6. MIT算法导论——第四讲.Quicksort

    本栏目(Algorithms)下MIT算法导论专题是个人对网易公开课MIT算法导论的学习心得与笔记.所有内容均来自MIT公开课Introduction to Algorithms中Charles E. ...

  7. Javascript中的冒泡排序,插入排序,选择排序,快速排序,归并排序,堆排序 算法性能分析

    阿里面试中有一道题是这样的: 请用JavaScript语言实现 sort 排序函数,要求:sort([5, 100, 6, 3, -12]) // 返回 [-12, 3, 5, 6, 100],如果你 ...

  8. 八大排序方法汇总(选择排序,插入排序-简单插入排序、shell排序,交换排序-冒泡排序、快速排序、堆排序,归并排序,计数排序)

    2013-08-22 14:55:33 八大排序方法汇总(选择排序-简单选择排序.堆排序,插入排序-简单插入排序.shell排序,交换排序-冒泡排序.快速排序,归并排序,计数排序). 插入排序还可以和 ...

  9. 排序算法——QuickSort、MergeSort、HeapSort(C++实现)

    快速排序QuickSort template <class Item> void quickSort (Item a[], int l, int r) { if (r<=l) ret ...

  10. 【从零学习经典算法系列】分治策略实例——高速排序(QuickSort)

    在前面的博文(http://blog.csdn.net/jasonding1354/article/details/37736555)中介绍了作为分治策略的经典实例,即归并排序.并给出了递归形式和循环 ...

随机推荐

  1. jchat:linux聊天程序4:客户端

    makefile: jchat: main.o login.o regist.o tcp.o gcc -w main.o login.o regist.o tcp.o -o jchat rm -f * ...

  2. python 网络编程第二版

    为服务端增加多进程解决方案 1.server端代码如下: #!/usr/bin/python #!coding:utf-8 import os,sys,time from socket import ...

  3. Linux突然断电后文件丢失的问题

      原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://yuyongid.blog.51cto.com/10626891/168504 ...

  4. 用 Python脚本生成 Android SALT 扰码

    发布Android 有偿应用时需要随机生成 SALT 扰码夹在文件中,以下是 Python脚本(当然你选择 C/Java/SHELL/Perl 或别的都行) #!/usr/bin/python # F ...

  5. 利用boost做string到wstring转换,以及字符集转换 - Error - C++博客

    利用boost做string到wstring转换,以及字符集转换 - Error - C++博客 利用boost做string到wstring转换,以及字符集转换 #include <boost ...

  6. HDU Today(dijskra)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. 如何用C#把Doc文档转换成rtf格式

    先在项目引用里添加上对Microsoft Word 9.0 object library的引用 using System; namespace DocConvert { class DoctoRtf ...

  8. 读取系统执行状态的shell脚本

    近期在学习shell.老大让写一个读取系统配置信息的脚本当作练习和工作验收,我就写了这么一个脚本,读取操作系统,内核,网卡,cpu,内存,磁盘等信息,目的是让看的人一眼就能看出这台机子的配置以及眼下的 ...

  9. 开发汉澳即时通信网,2006年上线,QQ死期到了

    为汉澳sinox用户打造即时通信网让大家用上即时通信软件 近期腾讯关闭了linuxQQ登录,汉澳 sinox也登陆不上.非windows用户再也不能用上即时通信软件了! 这是多么可悲的事,可是我们必须 ...

  10. vertical-align:middle的居中细节调整

    使用vertical-align:middle可以让行级元素垂直居中,但这个居中是以文字的中线来计算的,而文字的中线在不同的字体上不同,甚至相同的字体在不同的浏览器上显示的都不同.所以直接使用vert ...