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)中介绍了作为分治策略的经典实例,即归并排序.并给出了递归形式和循环 ...
随机推荐
- USB OTG学习
1. 概要 OTG设备使用插头中的ID引脚来区分A/B Device,ID接地被称作为A-Device,为连接时候的USB Host,A-Device始终为总线提供电力,ID悬空被称作为B-Devic ...
- KEIL MDK环境下uCOS-II在LPC17xx上的移植实例
1. 知识准备 要想对ucos-ii的移植有较深的理解,需要两方面知识: (1)目标芯片,这里是lpc17xx系列芯片,它们都是基于ARMv7 Cortex-M3内核,所以这一类芯片的ucos-ii移 ...
- c语言结构体排序示例
设计性实验编程实现对学生成绩表的相关信息排序.实验要求:⑴ 建立一个由n个学生的考试成绩表,每条信息由学号.姓名和分数组成.⑵ 按学号排序,并输出排序结果.⑶ 按分数排序,分数相同的则按学号有序,并输 ...
- 经典排序算法(Java实现)
以下程序均将数据封装于DataWrap数据包装类中,如下所示: //数据包装类 class DataWrap implements Comparable<DataWrap> { int d ...
- 「花田对」CSDN程序员专场——谁来拯救技术宅!_豆瓣
「花田对」CSDN程序员专场--谁来拯救技术宅!_豆瓣 「花田对」CSDN程序员专场--谁来拯救技术宅!
- 怎样在小方框上打对号 小方框内打对勾 word 方框打对勾
在word中做选择时,非常多人遇到须要在小方框上打对勾而不知怎样做,现将可行的各种方法总结例如以下: 1:直接找到一个做好的,保存为图片,在须要的时候插入它: 2:插入文本框,然后边框选择为实线,在文 ...
- android setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds差别
手工设置文本与图片相对位置时.经常使用到例如以下方法: setCompoundDrawables(left, top, right, bottom) setCompoundDrawablesWithI ...
- Mysql 计算时间间隔函数
#计算两个时间的间隔 #计算间隔天数 select TIMESTAMPDIFF(day,'2014-06-01',date(now())) #计算间隔月数 select TIMESTAMPDIFF(m ...
- Android开发8:UI组件TextView,EditText,Button
版本:Android4.3 API18 学习整理:liuxinming TextView 概述 TextView直接继承了View(EditText.Button两个UI组件类的父类) TextVie ...
- Ubuntu Code::Blocks IDE 13.12 汉化
Ubuntu Code::Blocks IDE 13.12 汉化: 安装很简单,不再赘述. 单说汉化: .下载中文简体汉化包(百度网盘):链接: http://pan.baidu.com/s/1kU3 ...