Brainman
Time Limit:1000MS Memory Limit:30000K
Total Submissions:13754 Accepted:6846
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 ofN 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:2803swap(28)8203swap(20)8023swap(23)8032swap(80)0832swap(83)0382swap(82)0328swap(32)0238swap(38)0283swap(83)0238
So the sequence(2803) can be sorted with nine swaps of adjacent numbers. However, it is even possible to sort it with three such swaps:
Start with:2803swap(80)2083swap(20)0283swap(83)0238
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 inthis 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
4428031001234567896-4223628-10065537500000
Sample Output
Scenario #1:3
Scenario #2:0
Scenario #3:5
Scenario #4:0
Source
TUD Programming Contest 2003, Darmstadt, Germany
代码
//机试之归并排序求逆序数——POJ1804Brainman
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;const int MAXN=1010;
int number;//记录逆序数对数目 voidCombine(int arr[],int left,int right){
int middle = left +(right - left)/2;
int i = left,j = middle +1;
int temp[MAXN],k = left;while(i <= middle && j <=right){if(arr[i]<= arr[j]){
temp[k++]= arr[i++];}else{
number += middle - i +1;
temp[k++]= arr[j++];}}while(i <= middle){
temp[k++]= arr[i++];}while(j <= right){
temp[k++]= arr[j++];}for(k = left; k <= right; k++){
arr[k]= temp[k];}}voidMergeSort(int arr[],int left,int right){// int middle = (left + right) / 2;if(left < right){
int middle = left +(right - left)/2;//防止数字越界 MergeSort(arr,left,middle);MergeSort(arr,middle+1,right);Combine(arr,left,right);}}voiddisplay(int arr[],int len){for(int i =0; i < len -1;++i){
cout<<arr[i]<<" ";}
cout<<arr[len -1]<<endl;}
int main(){
int caseNumber;
cin>>caseNumber;
int num[MAXN];for(int i =1;i <= caseNumber;++i){
int n;
cin>>n;for(int j =0; j < n;++j){
cin>>num[j];}
number =0;MergeSort(num,0,n-1);
cout<<"Scenario #"<<i<<":"<<endl;
cout<<number<<endl;
cout<<endl;}return0;}