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