poj 1804 (归并排序求逆序数)Bra…

来源:互联网 发布:java queue 编辑:程序博客网 时间:2024/06/05 09:55
Brainman
Time Limit: 1000MSMemory Limit: 30000KTotal Submissions: 6471Accepted: 3585

Description

Background 
Raymond Babbitt drives his brother Charlie mad. Recently Raymondcounted 246 toothpicks spilled all over the floor in an instantjust by glancing at them. And he can even count Poker cards.Charlie would love to be able to do cool things like that, too. Hewants to beat his brother in a similartask. 

Problem 
Here's what Charlie thinks of. Imagine you get a sequence of Nnumbers. The goal is to move the numbers around so that at the endthe sequence is ordered. The only operation allowed is to swap twoadjacent 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 adjacentnumbers. However, it is even possible to sort it with three suchswaps: 
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 adjacentnumbers to sort a given sequence?Since Charlie does not haveRaymond's mental capabilities, he decides to cheat. Here is whereyou come into play. He asks you to write a computer program for himthat answers the question. Rest assured he will pay a very goodprize for it.

Input

The first line contains the number ofscenarios. 
For every scenario, you are given a line containing first thelength N (1 <= N <= 1000) of thesequence,followed by the N elements of the sequence (each elementis an integer in [-1000000, 1000000]). All numbers in this line areseparated by single blanks.

Output

Start the output for every scenario with a linecontaining "Scenario #i:", where i is the number of the scenariostarting at 1. Then print a single line containing the minimalnumber of swaps of adjacent numbers that are necessary to sort thegiven sequence. Terminate the output for the scenario with a blankline.

Sample Input

4 2 8 0 3 
10 0 1 2 3 4 5 6 7 8 96 -42 23 
6 28 -100 65537 
5 0 0 0 0 0

Sample Output

Scenario #1: 
Scenario #2: 
Scenario #3: 
Scenario #4: 
0

这题是要求数列的逆序数。所谓数列的逆序数就是一个值sumsum=b[0]+b[1]+...+b[n-1]。这里假设数列为a,其中b[i]表示在数列中在a[i]后面并且比a[i]小的数的个数。比如有数列 2 8 0 3的逆序数就是1+2+0+0=3


求在数列的逆序数可以使用归并排序和数状数组求解,下面分别说明。


归并排序是将数列a[l,h]分成两半a[l,mid]a[mid+1,h]分别进行归并排序,然后再将这两半合并起来。在合并的过程中(设l<=i<=midmid+1<=j<=h),当a[i]<=a[j]时,并不产生逆序数;当a[i]>a[j]时,在前半部分中比a[i]大的数都比a[j]大,将a[j]放在a[i]前面的话,逆序数要加上mid+1-i。因此,可以在归并排序中的合并过程中计算逆序数。


数状数组求解时,要从数列的后面向前扫描a[i],依次计算a[i]后面的比a[i]小的数的个数并且累加起来。在这道题中,数列中的元素是[-10000001000000],要映射到[12000001]上。

归并排序法:
#include<stdio.h>
#include<malloc.h>
__int64 k=0;
int a[1005];
void Merge(int *R,int low,int m,int high)
    {
     int i=low,j=m+1,p=0;
     int *R1; 
     R1=(int *)malloc((high-low+1)*sizeof(int));   
     while(i<=m&&j<=high) 
         {
              if(R[i]<=R[j])
          {
                      R1[p++]=R[i++];
                    
               }
              else
           {
                      R1[p++]=R[j++];
                        k+=(m-i+1);
            }
      }
     while(i<=m) 
       R1[p++]=R[i++];
     while(j<=high) 
       {
              R1[p++]=R[j++];            
        }
              
     for(p=0,i=low;i<=high;p++,i++)
       R[i]=R1[p];
       free(R1);
    
 void MergeSortDC(int *R,int low,int high)
     {
       int mid;
       if(low<high)
           {
          mid=(low+high)/2;
          MergeSortDC(R,low,mid); 
          MergeSortDC(R,mid+1,high); 
          Merge(R,low,mid,high); 
        }
     }
 int main()
 {
       int i,j,N,c=1;     
        scanf("%d",&c);
for(i=1;i<=c;i++)
 {
              k=0;
           scanf("%d",&N);
                for(j=0;j<N;j++)
                 scanf("%d",&a[j]);
     MergeSortDC(a,0,N-1);       
      printf("Scenario #%d:\n%d\n\n",i,k);
  }
       return 0;
  
 }
树状数组法:
#include<stdio.h>
#include<string.h>
int N=2000001;
int C[2000005];
int a[1005];
int Lowbit(int x)
{
    return x&(-x);
}
void Modify(int i,int x)
{
    while(i<=N)
    {
        C[i]+=x;
        i+=Lowbit(i);
    }
}
int Sum(int i)
{
    int sum=0;
    while(i>0)
    {
        sum+=C[i];
        i-=Lowbit(i);
    }
    return sum;
}
int main()
{
    int c,t,n;
    int i,j;   
    int sum=0;    
    scanf("%d",&c);
    for(i=1;i<=c;++i)
    {
        sum=0;
        memset(C,0,sizeof(C));
        scanf("%d",&n);
        for(j=1;j<=n;++j)
        {
            scanf("%d",&a[j]);
        }
        for(j=n;j>=1;--j)
        {
            t=a[j]+1000001;
            sum+=Sum(t-1);
            Modify(t,1);
        }
        printf("Scenario #%d:\n%d\n\n",i,sum);
    }
    return 0 ;
}