Ultra-QuickSort(归并排序)
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 49267 | Accepted: 18035 |
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
题解,错了半天,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(归并排序)的更多相关文章
- 归并排序(MergeSort)和快速排序(QuickSort)的一些总结问题
归并排序(MergeSort)和快速排序(QuickSort)都是用了分治算法思想. 所谓分治算法,顾名思义,就是分而治之,就是将原问题分割成同等结构的子问题,之后将子问题逐一解决后,原问题也就得到了 ...
- Javascript算法系列之快速排序(Quicksort)
原文出自: http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/ https://gis ...
- ACM/ICPC 之 快排+归并排序-记录顺序对(TSH OJ-LightHouse(灯塔))
TsingHua OJ 上不能使用<algorithm>头文件,因此需要手写快排(刚开始写的时候自己就出了很多问题....),另外本题需要在给横坐标排序后,需要记录纵坐标的顺序对的数量,因 ...
- Ultra-QuickSort【归并排序典型题目】
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 34470 Accepted: 12382 ...
- leetcode:Sort List(一个链表的归并排序)
Sort a linked list in O(n log n) time using constant space complexity. 分析:题目要求时间复杂度为O(nlogn),所以不能用qu ...
- MIT算法导论——第四讲.Quicksort
本栏目(Algorithms)下MIT算法导论专题是个人对网易公开课MIT算法导论的学习心得与笔记.所有内容均来自MIT公开课Introduction to Algorithms中Charles E. ...
- Javascript中的冒泡排序,插入排序,选择排序,快速排序,归并排序,堆排序 算法性能分析
阿里面试中有一道题是这样的: 请用JavaScript语言实现 sort 排序函数,要求:sort([5, 100, 6, 3, -12]) // 返回 [-12, 3, 5, 6, 100],如果你 ...
- 八大排序方法汇总(选择排序,插入排序-简单插入排序、shell排序,交换排序-冒泡排序、快速排序、堆排序,归并排序,计数排序)
2013-08-22 14:55:33 八大排序方法汇总(选择排序-简单选择排序.堆排序,插入排序-简单插入排序.shell排序,交换排序-冒泡排序.快速排序,归并排序,计数排序). 插入排序还可以和 ...
- 排序算法——QuickSort、MergeSort、HeapSort(C++实现)
快速排序QuickSort template <class Item> void quickSort (Item a[], int l, int r) { if (r<=l) ret ...
- 【从零学习经典算法系列】分治策略实例——高速排序(QuickSort)
在前面的博文(http://blog.csdn.net/jasonding1354/article/details/37736555)中介绍了作为分治策略的经典实例,即归并排序.并给出了递归形式和循环 ...
随机推荐
- UML-类图,包图
UML构造设计模型 一.类图 二.包图 三.组件图 四.部署图 一.类图 1.类:类由三格表示:类名,类的属性,类的操作 类名: 首字母大学 ...
- SetThreadAffinityMask设置线程亲缘性
The SetThreadAffinityMask function sets a processor affinity mask for the specified thread. DWORD_PT ...
- 只有电信3G是公网ip。
只有电信3G是公网ip,其它网络拿到是内部网.
- Android stagefright与opencore对比
[转载至其它博客] http://blog.csdn.net/djy1992/article/details/9339917 1引言 Android froyo版本多媒体引擎做了变动,新添加了st ...
- 使用 Node.js 做 Function Test
Info 上周 meeting 上同事说他们现在在用 java 写 function test,产生了很多冗余的代码,整个项目也变得比较臃肿.现在迫切需要个简单的模板项目能快速搭建function t ...
- WinForm 窗体与窗体相互嵌套
只要将要被潜逃的的窗体的TopLeve设置为Flase即可像普通的控件一样,被添加到另外一个窗体中,TopLeve:是否为顶级窗口,下面来看代码: public partial class TTFor ...
- json具体解释
<span style="font-size:12px;">function testJson() { var jsonData = { "firstName ...
- 为何与0xff进行与运算
为何与0xff进行与运算 在剖析该问题前请看如下代码 public static String bytes2HexString(byte[] b) { String ret = "" ...
- 提交时提示错误This Bundle is invalid.New apps and app updates submitted to the App Store must be built wit
this bundle is invalid . new apps and app updates submitted to the app store must be built with publ ...
- ListView实现上拉下拉刷新加载功能
第一步.首先在你项目中创建一个包存放支持下拉刷新和上拉加载的类: