博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ultra-QuickSort
阅读量:4663 次
发布时间:2019-06-09

本文共 2593 字,大约阅读时间需要 8 分钟。

Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 44489   Accepted: 16176

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

59105431230

Sample Output

60 题目大意就是让你计算一个冒泡排序中,需要交换的次数。 以为数据量较大,所以我们这里用到了Merge Sort :
1 #include
2 #include
3 int temp[500005] ; 4 int a[500005] ; 5 __int64 number ; 6 void MergeSort( int a[] , int fir , int end ) 7 { 8 int len = end - fir ; 9 if( len <= 1 )10 return ;11 int mid = fir + len/2 ;12 MergeSort( a , fir , mid ) ;13 MergeSort( a , mid , end ) ;14 int p1 = fir , p2 = mid ;15 for( int i = fir ; i < end ; i++ )16 {17 if( p1 == mid )18 {19 temp[i] = a[p2++] ;20 }21 else if( p2 == end )22 {23 temp[i] = a[p1++] ;24 }25 else26 {27 if( a[p1] >= a[p2] )28 {29 temp[i] = a[p2++] ;30 number += mid - p1 ;//在Merge Sort 排序中仅仅多加了这句话31 }32 else33 {34 temp[i] = a[p1++] ;35 }36 }37 }38 39 for(int i = fir ; i < end ; i++ )40 {41 a[i] = temp[i] ;42 }43 }44 int main()45 {46 // freopen("a.txt" ,"r" , stdin );47 int n ;48 while( scanf("%d" , &n ) != EOF )49 {50 if( n == 0 ) break;51 number = 0 ;52 for(int i = 0 ; i < n ; i++ )53 scanf("%d" , &a[i] ) ;54 MergeSort( a , 0 , n ) ;55 56 printf("%I64d\n" , number );57 }58 return 0 ;59 }
AC

 

转载于:https://www.cnblogs.com/get-an-AC-everyday/p/4266074.html

你可能感兴趣的文章
Python之常用模块学习(一)
查看>>
CSS------如何让大小不一样的div顶部对齐
查看>>
SOCKET.IO 前后端使用
查看>>
CodeForces 122G Lucky Array(一脸懵逼的树状数组)
查看>>
【开发实例】C#调用SAPI实现语音合成的两种方法
查看>>
Django实战(15):Django实现RESTful web service
查看>>
离散实验二
查看>>
使用sharepoint里Open with explorer功能
查看>>
通过模糊来弱化背景
查看>>
The Fourth Day
查看>>
NSString 比较(转)
查看>>
[hdu3631]背包或中途相遇法
查看>>
模块化开发(seajs)
查看>>
HDU1848 Fibonacci again and again 博弈 SG函数
查看>>
iOS-自建iPa应用分发平台
查看>>
12月2日站立会议
查看>>
【转载】详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别
查看>>
DBA笔记oracle undo_retention参数可动态修改
查看>>
123我爱你
查看>>
HDU 4033 Regular Polygon(几何 + 二分)
查看>>