归并排序与逆序对问题---(解题报告)POJ1804---Brainman

来源:互联网 发布:loadrunner java user 编辑:程序博客网 时间:2024/06/06 05:36

归并排序,主要是以分治思想进行组合排序,先分再和,在和的过程中排序,排序的思路如下:先给你两组同向有序数组,首先判断两组数的首位的大小,并将较小的数保留到一个新数组中,接下来再比较较小组数的第二位和另一组数的第一位,仍然保留较小的数,这样就保证新数组的有序,需要注意的是当任意一组数为空时,就自然将另一组数的剩下数接到新数组后,至于为何,相信很好理解;(具体代码见下方/(ㄒoㄒ)/~~)

逆序对,逆序对问题设 A 为一个有 n 个数字的有序集 (n>1),其中所有数字各不相同。如果存在正整数 i, j 使得 1 ≤ i < j ≤ n 而且 A[i] > A[j],则 A[i], A[j]这个有序对称为 A 的一个逆序对,也称作逆序数。
例如,数组(3,1,4,5,2)的逆序对有(3,1),(3,2),(4,2),(5,2),共4个。

接下来谈谈在归并排序中顺手求出数组逆序对数的方法;因为在排序的合并过程中是将两组有序数进行比较,所以根据逆序对的定义,在数组下标 i 小于 j 时,如果有 a[ i ] 大于 a[ j ] 就可以知道下标 i 后面所有数都与 a[ j ] 构成逆序数,只需添加一句求和语句即可求出逆序数了;

Brainman
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6471 Accepted: 3585
Description
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

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. Rest assured he will pay a very good prize for it.
Input
The first line contains the number of scenarios.
For every scenario, you are given a line containing first the length N (1 <= N <= 1000) of the sequence,followed by the N elements of the sequence (each element is an integer in [-1000000, 1000000]). All numbers in this line are separated by single blanks.
Output
Start the output for every scenario with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print a single line containing the minimal number of swaps of adjacent numbers that are necessary to sort the given sequence. Terminate the output for the scenario with a blank line.
Sample Input
4
4 2 8 0 3
10 0 1 2 3 4 5 6 7 8 9 6 -42 23
6 28 -100 65537
5 0 0 0 0 0
Sample Output
Scenario #1:
3
Scenario #2:
0
Scenario #3:
5
Scenario #4:
0

题目大意:给你一个数组,每次只变化相邻的两位,求最少变化次数将其变为有序的;
解题思路:
1.根据数的逆序数的定义,可分析出这个最少次数就应该是这个数的逆序对数,例如,对2083这个数,它的变化过程如下:2083->0283->0238,经过了两步,易知逆序数也为2;
2.明确是求逆序数后,可以有两种方法(目前只知道两种orzorz),一是直接通过两重循环直接求出逆序数,二是通过归并排序求逆序对的方法求出;两种方法均可。

具体代码:
第一种直接求解:

#include <stdio.h>#include <string.h>const int N=1005;int main(){    int a[N];    memset(a,0,sizeof(0));    int T,sum=0,n,i,j,k=1;    scanf("%d",&T);    while (T--)       //记得每个例子结束后将sum清0, 否则将出错!!!!!!     {        scanf("%d",&n);        for(i=0;i<n;i++)        {            scanf("%d",&a[i]);        }           for(i=0;i<n;i++)        {            for(j=i+1;j<n;j++)            {                if(a[i]>a[j])                {                    sum++;                }            }        }        printf("Scenario #%d:\n",k++);        printf("%d\n\n",sum);        sum=0;    }    return 0;}

第二种利用归并排序求逆序对的方法:

#include <stdio.h>int a[1005],sum;void merge_sort(int l,int m,int r)   //因为起点和终点不变所以用i,j来表示可变的下标 {    int k=l,i=l,j=m+1;     int t[1005];    while(i<=m&&j<=r)    {        if(a[i]>a[j])     //前面的比后面的大,所以构成逆序对         {            sum=sum+m-i+1; //当i<j并且a[i]<a[j]自然通过,而当a[i]>a[j]就是一对逆序对,而从i到middle的值都大于a[i],因此自然都大于a[j],所以数量为middle-i+1(包括i自身),即只需在归并排序中加一句即可            t[k++]=a[j++];          }        else        {            t[k++]=a[i++];        }    }    while(i<=m)           //装剩下的     {        t[k++]=a[i++];    }    while(j<=r)    {        t[k++]=a[j++];    }    for ( i=l;i<=r;i++)    {        a[i]=t[i];    }}void merge(int l,int r){    if(l<r)   /*为何使用if不是while !!! 虽然二者都有条件判断,但是while是用来做循环的,也就是说只要条件满足,就会执行一次循环体,执行完以后会再判断一次条件,如果满足条件,还会再执行一次,终而复始,除非你在循环中对条件进行了改变才会从循环中跳出来。而if只做一次判断,条件不满足就不执行,满足就执行一次,执行完就往下执行,不会再回过头来继续执行。*/     {        int m=(l+r)/2;        merge(l,m);        merge(m+1,r);         merge_sort(l,m,r);    }}//归并排序与逆序对的关系int main() {    int T,n,i=1,j;    scanf("%d",&T);    while(T--)    {        sum=0;        scanf("%d",&n);        for(j=0;j<n;j++)        scanf("%d",&a[j]);        merge(0,n-1);        printf("Scenario #%d:\n",i++);        printf("%d\n\n",sum);    }    return 0;}

仅代表个人观点,不喜勿喷,欢迎交流!
这里写图片描述

0 0
原创粉丝点击