翻译题16

Sorting by Swapping【交换排序】

总时间限制: 

1000ms

内存限制: 

65536kB

描述

Given a permutation of numbers from 1 to n, we can always get the sequence 1, 2, 3, ..., n by swapping pairs of numbers. For example, if the initial sequence is 2, 3, 5, 4, 1, we can sort them in the following way:

【给出一组数1至n,通过交换数据我们总能得到这样的排序1,2,3,4...,n,例如,如果初始顺序是2,3,5,4,1,我们可以通过排序得出以下】

2 3 5 4 1
1 3 5 4 2
1 3 2 4 5
1 2 3 4 5

Here three swaps have been used. The problem is, given a specific permutation, how many swaps we needs to take at least.

【这发生了三次交换,问题是,给出一组具体的数,我们应该至少交换几次】

输入

The first line contains a single integer t (1 <= t <= 20) that indicates the number of test cases. Then follow the t cases. Each case contains two lines. The first line contains the integer n (1 <= n <= 10000), and the second line gives the initial permutation.

【第一行包含一个整数t(1 <= t <= 20),表示有t组测试样例。接下来有t组数,每组数包含两行,第一行包含整数n(1 <= n <= 10000),第二行则表示初始数据。】

输出

For each test case, the output will be only one integer, which is the least number of swaps needed to get the sequence 1, 2, 3, ..., n from the initial permutation.

【对于每组测试数据仅输出一个整数,为最后得出1,2,3....,n顺序要至少交换次数。】

样例输入

2
3
1 2 3
5
2 3 5 4 1

样例输出

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值