Chinaunix首页 | 论坛 | 博客
  • 博客访问: 350633
  • 博文数量: 122
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 1191
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 11:12
文章分类

全部博文(122)

文章存档

2010年(122)

我的朋友

分类: C/C++

2010-04-24 22:31:14

一、问题描述

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

 

二、解题思路

这题是要求数列的逆序数。所谓数列的逆序数就是一个值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<iostream>
using namespace std;
int a[1005];
int b[1005];
int sum=0;
void merge(int a[],int low, int mid, int high)
{
    int i=low;
    int j=mid+1;
    int k=0;
    while(i<=mid && j<=high)
    {
        if(a[i] <= a[j] )
        {
            b[k++]=a[i++];
        }
        else
        {
            b[k++]=a[j++];
            sum+=(mid-i+1);
        }
    }
    while(i<=mid)
        b[k++]=a[i++];
    while(j<=high)
        b[k++]=a[j++];
    for(i=0,j=low;j<=high;i++,j++)
        a[j]=b[i];
}
void mergesort(int a[],unsigned int st,unsigned int ed)
{
    unsigned int mid =0;
    if(st < ed)
    {
        mid = (st+ed)/2;
        mergesort(a,st,mid);
        mergesort(a,mid+1,ed);
        merge(a,st,mid,ed);
    }
}
int main()
{
    int c=1;
    int i,j;
    int N;
    scanf("%d",&c);
    for(i=1;i<=c;i++)
    {
        scanf("%d",&N);
        for(j=0;j<N;++j)
            scanf("%d",&a[j]);
        sum=0;
        mergesort(a,0,N-1);
        printf("Scenario #%d:\n%d\n\n",i,sum);
    }
    return 0;
}


四、树状数组求逆序数

 

#include<iostream>
using namespace std;
int N=2000001;
int C[2000005];
int a[1005];
int Lowbit(int x)
{
    return x&(x^(x-1));
}
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;
    int i,j;
    int t;
    int sum=0;
    int n;
    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 ;
}


阅读(2069) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~