poj 1804 (归并排序求逆序数)Brainman

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 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: 
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 ;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值