Program A-归并排序

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<iostream>

using namespace std;

long long  cnt;

void merge(int array[],int left,int mid,int right)
{
    int* temp=new int[right-left+1];
    int i,j,p;
    for(i=left,j=mid+1,p=0;i<=mid&&j<=right;p++)
    {
        if(array[i]<=array[j])temp[p]=array[i++];
        else
        {
            temp[p]=array[j++];cnt+=(mid-i+1);
        }
    }
    while(i<=mid)temp[p++]=array[i++];
    while(j<=right)temp[p++]=array[j++];
    for(i=left,p=0;i<=right;i++)array[i]=temp[p++];
    delete temp;
}

void mergesort(int array[],int left,int right)
{
    if(left==right)array[left]=array[right];
    else
    {
        int mid=(left+right)/2;
        mergesort(array,left,mid);
        mergesort(array,mid+1,right);
        merge(array,left,mid,right);
    }
}
int main()
{
    int n,array[500005];
    while(cin>>n,n)
    {
        cnt=0;
        for(int i=0;i<n;i++)
            cin>>array[i];
        mergesort(array,0,n-1);
            cout<<cnt<<endl;
    }
    return 0;
}
时间: 2024-10-04 00:04:55

Program A-归并排序的相关文章

POJ 2299 Ultra-QuickSort(归并排序&#183;逆序对)

题意  给你一个数组求其中逆序对(i<j&&a[i]>a[j])的个数 我们来看一个归并排序的过程: 给定的数组为[2, 4, 5, 3, 1],二分后的数组分别为[2, 4, 5], [1, 3],假设我们已经完成了子过程,现在进行到该数组的"并"操作: a: [2, 4, 5] b: [1, 3] result:[1] 选取b数组的1 a: [2, 4, 5] b: [3] result:[1, 2] 选取a数组的2 a: [4, 5] b: [3] r

POJ 2299 求逆序对(归并排序或树状数组)

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 45290   Accepted: 16440 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

POJ 2299 Ultra-QuickSort (求逆序数:离散化+树状数组或者归并排序求逆序数)

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 55048   Accepted: 20256 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

Ultra-QuickSort——[归并排序、分治求逆序对]

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 seque

Ultra-QuickSort(归并排序+离散化树状数组)

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 50517   Accepted: 18534 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)

Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10575   Accepted: 5489 Description BackgroundRaymond Babbitt drives his brother Charlie mad. Recently Raymond counted 246 toothpicks spilled all over the floor in an instant just b

SRM 664 Div2 Hard: BearSortsDiv2(归并排序)

Problem Statement   Bear Limak was chilling in the forest when he suddenly found a computer program. The program was a correct implementation of MergeSort. Below you can find the program in pseudocode. # mergeSort(left,right) sorts elements left, lef

A 归并排序

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

POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 39279   Accepted: 14163 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

poj2299.4thweek.p.A.归并排序

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 sequ