poj 2299 Ultra-QuickSort(归并排序)

题目:http://poj.org/problem?id=2299

Ultra-QuickSort
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 49165 Accepted: 17984

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
本来想用这题来练习线段树的,结果怎么也想不到那里去。后来看别人的代码才明白每个叶子节点存储的不是依据dex存储val[i]而是依据dex在相应位置存储1,然后根据排好序的序列由val[i]追踪dex,左边的1都加起来(逆序数),然后去掉dex的1,如此反复进行就能到达目的。提交代码时我可没这么做,用归并排序累加每一次相邻的交换数就是最终的结果。(临时在百度百科上学习了归并排序||-_-)
冒泡排序的时间的复杂度是O(n^2).
冒泡排序算法的运作如下:(从后往前)
比较相邻的元素。如果第一个比第二个大,就交换他们两个。
对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。
针对所有的元素重复以上的步骤,除了最后一个。
持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。
并归排序的时间复杂度:O(nlogn)
设有数列{6,202,100,301,38,8,1}
初始状态:6,202,100,301,38,8,1
第一次归并后:{6,202},{100,301},{8,38},{1},比较次数:3;
第二次归并后:{6,100,202,301},{1,8,38},比较次数:4;
第三次归并后:{1,6,8,38,100,202,301},比较次数:4;
总的比较次数为:3+4+4=11,;
逆序数为14;
归并排序的优势:分治法,已比较了的小部分不再比较,如:{8,38},{1}变成{1,8,38}只比较1和8后{1}的长度成为0,不再比较,直接写下{8,38}。

#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=5e5+10;
int a[maxn],b[maxn];
long long sum;
void Merge(int sourceArr[],int tempArr[],int startdex,int middex,int enddex)
{
    int i = startdex,j=middex+1,k = startdex;
    while(i<=middex && j<=enddex)
    {
        if(sourceArr[i]<sourceArr[j])
            tempArr[k++] = sourceArr[i++];
        else
            tempArr[k++] = sourceArr[j++],sum+=(middex+1-i);  //if sum++; not adjacent exchange
    }
    while(i!=middex+1)
        tempArr[k++] = sourceArr[i++];
    while(j!=enddex+1)
        tempArr[k++] = sourceArr[j++];
    for(i=startdex;i<=enddex;i++)
        sourceArr[i] = tempArr[i];
}

void MergeSort(int sourceArr[],int tempArr[],int startdex,int enddex)
{
    int middex;
    if(startdex<enddex)
    {
        middex=(startdex+enddex)/2;
        MergeSort(sourceArr,tempArr,startdex,middex);
        MergeSort(sourceArr,tempArr,middex+1,enddex);
        Merge(sourceArr,tempArr,startdex,middex,enddex);
    }
}
int main()
{
    //freopen("cin.txt","r",stdin);
    int n;
    while(cin>>n&&n){
        sum=0;
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        MergeSort(a,b,0,n-1);
        printf("%lld\n",sum);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值