【PTA】7-2 求逆序对数目 (30 分)

7-2 求逆序对数目 (30 分)

注意:本问题算法的时间复杂度要求为O(nlogn), 否则得分无效

题目来源:http://poj.org/problem?id=1804
Background Raymond Babbitt drives his brother Charlie mad. Recently Raymond counted 246 toothpicks spilled all over the floor in an instant just by glancing at them. And he can even count Poker cards. Charlie would love to be able to do cool things like that, too. He wants to beat his brother in a similar task.
Problem Here’s what Charlie thinks of. Imagine you get a sequence of N numbers. The goal is to move the numbers around so that at the end the sequence is ordered. The only operation allowed is to swap two adjacent numbers. Let us try an example: Start with: 2 8 0 3 swap (2 8) 8 2 0 3 swap (2 0) 8 0 2 3 swap (2 3) 8 0 3 2 swap (8 0) 0 8 3 2 swap (8 3) 0 3 8 2 swap (8 2) 0 3 2 8 swap (3 2) 0 2 3 8 swap (3 8) 0 2 8 3 swap (8 3) 0 2 3 8

翻译:
这是查理的想法。假设你得到一个有N个数字的序列。其目标是移动数字,以便在最后使序列有序。唯一允许的操作是交换两个相邻的数。让我们试试一个例子:
首先:2 8 0 3互换(2 8)
8 2 0 3互换(2 0)
8 0 2 3互换(2 3)
8 0 3 2交换(8 0)
0 8 3 2交换(8 3)
0 3 8 2交换(8 2)
0 3 2 8交换(3 2)
0 2 3 8交换(3 8)
0 2 8 3互换(3 8)
最后得到 0 2 3 8

So the sequence (2 8 0 3) can be sorted with nine swaps of adjacent numbers. However, it is even possible to sort it with three such swaps: Start with: 2 8 0 3 swap (8 0) 2 0 8 3 swap (2 0) 0 2 8 3 swap (8 3) 0 2 3 8
The question is: What is the minimum number of swaps of adjacent numbers to sort a given sequence?Since Charlie does not have Raymond’s mental capabilities, he decides to cheat. Here is where you come into play. He asks you to write a computer program for him that answers the question in O(nlogn). Rest assured he will pay a very good prize for it.

翻译:
所以序列(2 8 0 3)可以用9次相邻数字的交换来排序。然而,它甚至可以用三个这样的交换来排序:
开始:2 8 0 3 交换 (8 0)
2 0 8 3 交换 (2 0)
0 2 8 3 交换 (8 3)
得到0 2 3 8

输入格式:

The first line contains the length N (1 <= N <= 1000) of the sequence; The second line contains the N elements of the sequence (each element is an integer in [-1000000, 1000000]). All numbers in this line are separated by single blanks.

翻译:第一行包含序列的长度N (1 <= N <= 1000);
第二行包含序列的N个元素(每个元素是[-1000000,1000000]的整数)。
这一行的所有数字用单个空格隔开

输出格式:

Print a single line containing the minimal number of swaps of adjacent numbers that are necessary to sort the given sequence.

翻译:打印出排序给定序列所需的相邻数字的最小交换数

输入样例:

在这里给出一组输入。例如:

6
-42 23 6 28 -100 65537
结尾无空行

输出样例:

在这里给出相应的输出。例如:

5 结尾无空行

思路:

1.因为复杂度要求为O(nlogn),所以采用合并排序的方法。
2.题目标题为逆序对,逆序对就是 <a[i] , a[j]> ,其中 i<j , 但a[i]>a[j]
3.第一步使用mergesort()将数组分成两半排好序,然后递归调用。
第二步使用merge()将分开的一个个数组重新拼接在一起。
4.merge()里面是将完整的数组a分成两半,分别从两半的第一个数字,即 a[i]和a[j]开始比较大小,将较小的数组放入数组b。而且,如果a[j]的左边有大于a[j]的数字(且没有进入数组b),则说明可以构成逆序对,这样的数字的个数记为num。
5.主函数输出num。

#include<iostream>
using namespace std;
int num;//逆序对个数
int a[10005],b[10005];

int merge(int a[],int b[],int l,int m,int r){
    
    
    int i=l,j=m+1,k=l;//k:数组b的下标
    //将2803分成两个数组(且两数组已经分别排好序),前面一半28,后面一半03,从两个数组的第一个数字开始比较,所以i=l,j=m+1
    while((i<=m)&&(j<=r)){
    
    
        if(a[i] <= a[j]){
    
    
            b[k++]=a[i++];//左右比较,将较小的数字放入数组b
        }else{
    
    
            b[k++]=a[j++];
            num+=m+1-i;//num表示在a[j]左边的数字中(除了已经进入b数组的数字),有多少个数字可以和a[j]组成逆序对(即比a[j]大)
        }
    }
    if(i>m){
    
    //将右边剩下的都填入b
        for(int q=j;q<=r;q++){
    
    
            b[k++]=a[q];
        }
    }else{
    
    //将左边剩下的都填入b
        for(int q=i;q<=i;q++){
    
    
            b[k++]=a[q];
        }
    }
    for(int q=l;q<=r;q++){
    
    //复制回a数组
        a[q]=b[q];    }
}

int mergesort(int a[],int l,int r){
    
    
    if(r>l){
    
    
        int m=(r+l)/2;
        mergesort(a,l,m);
        mergesort(a,m+1,r);
        merge(a,b,l,m,r);
    }
}

int main(){
    
    
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
    
    
        cin>>a[i];
    }
    mergesort(a,0,n-1);
    cout<<num;
}

猜你喜欢

转载自blog.csdn.net/qq_51669241/article/details/120469588