# poj1804 Brainman 归并排序求逆序对

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:

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

## 归并排序

### 求逆序对

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=2000;
int a[maxn<<1],temp[maxn<<1];
int ans,t,n;
void sort_m(int l,int r)
{
if(l==r) return;//如果l，r相等，说明区间内只有一个数字，此时可保证有序

int mid=(l+r)>>1;
sort_m(l,mid);
sort_m(mid+1,r);//递归分治左右区间

int p=l,cur=l,j=mid+1;
//cur为左区间指针，j为右区间的指针，p为temp数组指针；
while(cur<=mid&&j<=r)//指针在区间内
{
if(a[cur]>a[j])//右区间最小值小于左区间最小值
{
ans+=mid-cur+1;//逆序对数+=左区间大小
temp[p++]=a[j++];//右区间第一个元素放入temp数组，指针后移
}
else//右区间最小值大于左区间最小值，即当前暂时不存在逆序对
temp[p++]=a[cur++];//左区间第一个元素放入temp数组，指针后移
}
while(cur<=mid) temp[p++]=a[cur++];//右区间空，将左区间剩余元素全部放入temp中
while(j<=r) temp[p++]=a[j++];//左区间空，将右区间剩余元素全部放入temp中
for(cur=l;cur<=r;cur++) //将排好序的temp数组复制回a数组中，以完成归并排序
a[cur]=temp[cur];

}
int main(){
scanf("%d",&t);
for(int i=1;i<=t;i++)//poj神奇多组数据，记得清空数组
{
memset(a,0,sizeof a);ans=0;memset(temp,0,sizeof temp);//初始化
scanf("%d",&n);
for(int j=1;j<=n;j++)
scanf("%d",&a[j]);
printf("Scenario #%d:\n",i);
sort_m(1,n);//归并求逆序对
printf("%d\n",ans);
printf("\n");
}
return 0;
}       
• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

03-29 183

11-21 108
06-23 290
03-15 624
08-29 1379
07-31 542
08-28 361
03-16 48
©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客

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