WOJ1046-Crazy Game

What a hot day! The summer of Wuhan is always hot. One day, Flymouse and his classmates went to the natatorium together. The natatorium developed an amusing team game. They built a “bridge” in the center of the pool. The bridge only could hold two students. Strangely, it could be changed 180 degrees. By this way two students can change their orders when they are neighbors. Now, everyone of N students is given an unique number between 1 and N. After several changes, they form a queue by the orders:1  2  3  4 …… N.Flymouse is not clever enough and cannot solve this problem by the minimum times of changes. So he asks for your help.

输入格式

The first line contains a single integer T, the number of test cases, followed by two lines for each test case. The first line for
each test case contains a single integer N(0<N<=3000), the second line for each test case contains N numbers, the initial orders of the queue.

输出格式

There should be one output line per test case containing the minimum times of changes.

样例输入

1
4
4 3 2 1

样例输出

6
冒泡排序的交换次数就是逆序对的个数,找数列有多少逆序对即可。

#include<stdio.h>
int shu[3005];
int main(){
	int t,n,i,j,temp,ans;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		ans=0;
		for(i=0;i<n;i++)
		scanf("%d",&shu[i]);
		for(i=0;i<n-1;i++)
			for(j=i+1;j<n;j++)
				if(shu[i]>shu[j])
				ans++;
		printf("%d\n",ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值