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;
}


全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 12:10
点赞 评论 收藏
分享
Lorn的意义:你这标个前端是想找全栈吗?而且项目确实没什么含金量,技术栈太少了,边沉淀边找吧 现在学院本想就业好一点四年至少得高三模式两年加油吧
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务