码迷,mamicode.com
首页 > 其他好文 > 详细

解题报告——POJ 2299

时间:2015-02-11 18:06:47      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 44671   Accepted: 16240

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
----------------------------------------------------------------------------------------------------------------
题目是求逆序对,可以通过快排或者归并算法得出逆序数然后输出。
------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>

__int64 sum = 0;

void mergeSort(int arr[], int first, int last, int temp[])
{
    if (first < last)
    {
        int mid = (first + last) / 2;
        mergeSort(arr, first, mid, temp);
        mergeSort(arr, mid + 1, last, temp);
        mergeArray(arr, first, mid, last, temp);

    }
}
void mergeArray(int arr[], int first, int mid, int last, int temp[])
{
    int i = first, j = mid + 1;
    int m = mid, n = last;
    int k = 0;
    while (i <= m && j <= n)
    {
        if (arr[i] < arr[j])
        {
            temp[k++] = arr[i++];
        }
        else
        {
            temp[k++] = arr[j++];
            sum += m - i + 1;
        }
    }

    while (i <= m)
    {
        temp[k++] = arr[i++];
    }
    while (j <= n)
    {
        temp[k++] = arr[j++];
    }
    for (i = 0; i < k; i++)
    {
        arr[first + i] = temp[i];
    }
}

void MergeSort(int arr[], int n)
{
    int *p = (int *)malloc(500000);
    if (p)
    {
        mergeSort(arr, 0, n - 1, p);
        //free(p);
    }
}


int main()
{
    int head;
    int a[500000];
    //freopen("D:\\test.txt", "r", stdin);
    while(1)
    {
        scanf("%d", &head);
        if (head == 0) break;
        int i;
        for (i = 0; i < head; i++)
        {
            scanf("%d", &a[i]);
        }
        sum = 0;
        MergeSort(a, head);
        printf("%I64d\n", sum);

    }
    return 0;
}

 

解题报告——POJ 2299

标签:

原文地址:http://www.cnblogs.com/codingpenguin/p/4286585.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!