Given an array containing a permutation of 1 to n, you have to find the minimum number of swaps to sort the array in ascending order. A swap means, you can exchange any two elements of the array.

For example, let n = 4, and the array be 4 2 3 1, then you can sort it in ascending order in just 1 swaps (by swapping 4 and 1).

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains two lines, the first line contains an integer n (1 ≤ n ≤ 100). The next line contains n integers separated by spaces. You may assume that the array will always contain a permutation of 1 to n.

Output

For each case, print the case number and the minimum number of swaps required to sort the array in ascending order.

Sample Input

Output for Sample Input

3

4

4 2 3 1

4

4 3 2 1

4

1 2 3 4

Case 1: 1

Case 2: 2

Case 3: 0

#include <iostream>
#include <stdio.h>
using namespace std;
int a[];
int main() {
int t, n;
cin >> t;
for(int k = ; k <= t; ++k) {
cin >> n;
for (int i = ; i <= n; ++i) {
cin >>a[i];
}
int ans = ;
for (int i = ; i <= n; ++i) {
int x = i;
int cnt = ;
while (a[i] != i) {
x = a[i];
swap(a[x], a[i]);
cnt++;
}
ans += cnt;
}
printf("Case %d: %d\n",k, ans);
}
return ;
}

Lightoj 1166 - Old Sorting的更多相关文章

  1. LightOJ 1166 Old Sorting 置换群 或 贪心 水题

    LINK 题意:给出1~n数字的排列,求变为递增有序的最小交换次数 思路:水题.数据给的很小怎么搞都可以.由于坐标和数字都是1~n,所以我使用置换群求循环节个数和长度的方法. /** @Date : ...

  2. Old Sorting(转化成单调序列的最小次数,置换群思想)

     Old Sorting Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit S ...

  3. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  4. HDU Cow Sorting (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1  ...

  5. HDU 1166 敌兵布阵 (树状数组)

    题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=1166 敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    ...

  6. HDU 1166 敌兵布阵(分块)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  7. LightOj 1298 - One Theorem, One Year(DP + 欧拉)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1298 题意:给你两个数 n, p,表示一个数是由前 k 个素数组成的,共有 n 个素数 ...

  8. 1214 - Large Division -- LightOj(大数取余)

    http://lightoj.com/volume_showproblem.php?problem=1214 这就是一道简单的大数取余. 还想还用到了同余定理: 所谓的同余,顾名思义,就是许多的数被一 ...

  9. 1306. Sorting Algorithm 2016 12 30

    1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...

随机推荐

  1. jQuery.data() 的实现方式

    jQuery.data() 的作用是为普通对象或 DOM Element 附加(及获取)数据. 下面将分三个部分分析其实现方式:     1. 用name和value为对象附加数据:即传入三个参数,第 ...

  2. mybatis动态sql foreach的用法

    <select id="findUserByIds" parameterType="com.pojo.QueryVo" resultType=" ...

  3. xtu summer individual 1 E - Palindromic Numbers

    E - Palindromic Numbers Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %l ...

  4. NYOJ595乱七八糟好坑的水题~~

    乱七八糟 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述 一天,PIAOYI查看班级成绩时发现各种乱七八糟的数据,有点晕--但是他现在非常想知道排名情况,你能帮帮他吗?为 ...

  5. PTA 04-树6 Complete Binary Search Tree (30分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/669 5-7 Complete Binary Search Tree   (30分) A ...

  6. 【BZOJ2982】combination(Lucas定理)

    题意:求C(n,m) n,m<=200000000 思路:c(n,m)=c(n mod mo,m mod mo)*c(n div mo,m div mo) mod mo (n>=mo或m& ...

  7. ES6__class 的继承等相关知识案例

    /** * class 的继承等相关知识 */ // extends. static. super const canvas = document.querySelector('#canvas'); ...

  8. type和metaclass元类

    元类type 1. 创建类的两种方式 (都是由type元类创建) 方式一: class Foo(object): # 默认metaclass = type, 当前类, 由type类创建 a = 'aa ...

  9. Django学习之 - 基础ORM

    ORM操作参考: http://www.cnblogs.com/wupeiqi/articles/5246483.html 1:根据类自动创建数据库表,(类创建文件:models.py) 2:根据类对 ...

  10. 2018-2019 XIX Open Cup, Grand Prix of Korea (Division 2) GYM 102058 F SG函数

    http://codeforces.com/gym/102058/problem/F 题意:平面上n个点  两个人轮流在任意两个点之间连一条线但是不能和已有的线相交,先围成一个凸多边形的获胜,先手赢还 ...