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. luogu2633 Count on a tree

    主席树放到树上而已 #include <algorithm> #include <iostream> #include <cstdio> using namespa ...

  2. B/S 开发和 C/S开发的区别

    导读:每天都从应用中心下载很多软件安装尝试,在自己的电脑上也装了很多软件,但是,就出现了一个问题,好比QQ,为什么有了APP,还要有网站应用呢?由此,结合到自己的学习,就衍生出一个问题:C/S 开发就 ...

  3. 用SQLLDR来装载date类型的控制文件

    以前给山东某单位做oracle数据库恢复得时候,恢复出来得数据中包含date类型,当时给客户提供得是sqlldr得方式,因为数据量比较大,用sqlldr装载起来速度比较快,所以采用了这种方式,结果在装 ...

  4. [codeforces722D]Generating Sets

    [codeforces722D]Generating Sets 试题描述 You are given a set Y of n distinct positive integers y1, y2, . ...

  5. 在Ignite中使用线性回归算法

    在本系列前面的文章中,简单介绍了一下Ignite的机器学习网格,下面会趁热打铁,结合一些示例,深入介绍Ignite支持的一些机器学习算法. 如果要找合适的数据集,会发现可用的有很多,但是对于线性回归来 ...

  6. POJ3528移石头

    题目大意: 河道两旁直线上有两块石头不能移动,距离为L,但中间放置了N块石头并列出这N块石头到起点的距离,可以移走M块,那么移走石头后每次牛跨石头的最小距离如何达到最大值,输出这个最大值 让最小距离的 ...

  7. java中filter的用法

    filter过滤器主要使用于前台向后台传递数据是的过滤操作.程度很简单就不说明了,直接给几个已经写好的代码: 一.使浏览器不缓存页面的过滤器 Java代码   import javax.servlet ...

  8. ListView更新问题

    ListView和Adapter对象均具备有对象更新方法 ListView对象列表的更新方法1.invalidate();--重绘组件2.invlidateView()--重绘组件并包含所有的View ...

  9. php中memcache与memcached的区别 【收藏】

    说法一:    两个不同版本的php的memcached的客户端 new memcache是pecl扩展库版本new memcached是libmemcached版本功能差不多 说法二:    Mem ...

  10. 前端学习之- Ajax

    Ajax:页面不做刷新,直接将数据悄悄提交到后台,然后通过回调函数处理返回结果. $.Ajax({ # 提交到后台 url:'/host', # 提交到哪里 type:'POST' # 提交方式 da ...