POJ1804 Brainman【逆序偶+歸併排序】

Brainman

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 12463   Accepted: 6288

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

Source

TUD Programming Contest 2003, Darmstadt, Germany

 

問題鏈接POJ1804 Brainman

問題簡述:(略)

問題分析

  可以定義逆序對,一個序列中存在a[i]>a[j]且i<j,則a[i]與a[j]構成一對逆序對。

  一個序列的逆序對的總數,就是這個序列的逆序數。

  相鄰元素進行交換,那麼每次交換序列逆序數必然改變1,而一個遞增的序列逆序數爲0。因此,最少交換次數即爲逆序數,而每次按照逆序對減少的方式交換就得到遞增序列。

  這個程序必須採用基於分治法的排序算法程序,以便保證時間複雜度爲O(nlogn)。

程序說明

  使用暴力程序則容易出現TLE。採用分治法,使用歸併排序的思想來實現,計算複雜度降低。

  程序使用標準的歸併排序程序修改而成,只是增加了第19行。

  這次寫的程序,把16行改爲<=(原先是<)。這個是必要的,如果數的序列中出現相同值,這裏是需要改的。

題記:(略)

參考鏈接歸併排序(分治法)

 

AC的C語言程序如下:

/* POJ1804 Brainman */

#include <iostream>
#include <stdio.h>

using namespace std;

const int N = 1000;
int a[N], temp[N], cnt;

void merge(int low, int mid, int high)
{
    int i = low, j=mid+1, k = low;
    while(i <= mid && j <= high)
    {
        if(a[i] <= a[j])
            temp[k++] = a[i++];
        else {
            cnt += j - k;
            temp[k++] = a[j++];
        }
    }
    while(i <= mid)
        temp[k++] = a[i++];
    while(j <= high)
        temp[k++] = a[j++];
    for(i=low; i<=high; i++)
         a[i] = temp[i];
}

void mergesort(int low, int high)
{
    if(low < high)
    {
        int middle = (low + high) / 2;
        mergesort(low, middle);
        mergesort(middle+1, high);
        merge(low, middle, high);
    }
}

int main()
{
    int t, caseno = 0, n;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
            scanf("%d", &a[i]);

        cnt = 0;
        mergesort(0, n - 1);

        printf("Scenario #%d:\n%d\n\n", ++caseno, cnt);
    }

    return 0;
}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章