Old Sorting

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

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 nintegers 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

3

4

4 2 3 1

4

4 3 2 1

4

1 2 3 4

Sample Output

Case 1: 1

Case 2: 2

Case 3: 0

题解:求转化成单调序列的最小次数;蓝桥杯那题一样。。。当初竟然没写出来。。。

有置换群的思想,对于每一个循环,只需要交换num - 1次就好了;把所有的加上就好了;

代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<algorithm>
  5. #define mem(x, y) memset(x, y, sizeof(x))
  6. using namespace std;
  7. const int INF = 0x3f3f3f3f;
  8. const int MAXN = ;
  9. int vis[MAXN];
  10. struct Node{
  11. int pos,v;
  12. friend bool operator < (Node a, Node b){
  13. if(a.v != b.v){
  14. return a.v < b.v;
  15. }
  16. else return a.pos < b.pos;
  17. }
  18. };
  19. Node dt[MAXN];
  20. int main(){
  21. int N, kase = , T;
  22. scanf("%d", &T);
  23. while(T--){
  24. scanf("%d", &N);
  25. for(int i = ; i <= N; i++){
  26. scanf("%d", &dt[i].v);
  27. dt[i].pos = i;
  28. }
  29. sort(dt + , dt + N + );
  30. mem(vis, );
  31. int ans = ;
  32. for(int i = ; i <= N; i++){
  33. if(!vis[i]){
  34. int num = ;
  35. int j = i;
  36. while(!vis[j]){
  37. vis[j] = ;
  38. num++;
  39. j = dt[j].pos;
  40. }
  41. ans += num - ;
  42. }
  43. }
  44. printf("Case %d: %d\n", ++kase, ans);
  45. }
  46. return ;
  47. }

Old Sorting(转化成单调序列的最小次数,置换群思想)的更多相关文章

  1. 把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend

    //把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend //dp[i][j]:把第i个数转成第j小的数,最小花费 //此题与po ...

  2. CF713C Sonya and Problem Wihtout a Legend & hihocoder1942 单调序列

    这两个题是一样的,不过数据范围不同. 思路1: 在CF713C中,首先考虑使生成序列单调不下降的情况如何求解.因为单调上升的情况可以通过预处理将a[i]减去i转化成单调不下降的情况. 首先,生成的序列 ...

  3. 把一个序列转换成非严格递增序列的最小花费 POJ 3666

    //把一个序列转换成非严格递增序列的最小花费 POJ 3666 //dp[i][j]:把第i个数转成第j小的数,最小花费 #include <iostream> #include < ...

  4. POJ 3321 Apple Tree(后根遍历将树转化成序列,用树状数组维护)

    题意:一棵树,有很多分叉,每个分叉上最多有1个苹果. 给出n,接下来n-1行,每行u,v,表示分叉u,v之间有树枝相连.这里数据中u相当于树中的父节点,v相当于子节点. 给出两个操作: 1.C x  ...

  5. 51nod1476 括号序列的最小代价

    这题应该可以用费用流写吧?不过我想不出贪心来TAT.其实还是单调队列乱搞啊T_T //ÍøÉϵÄ̰ÐÄËã·¨ºÃÉñ°¡¡£¡£¡£ÎÒÖ»»áÓÃ×îС·ÑÓÃ×î´óÁ÷ÅÜTAT #in ...

  6. S - Making the Grade POJ - 3666 结论 将严格递减转化成非严格的

    S - Making the Grade POJ - 3666 这个题目要求把一个给定的序列变成递增或者递减序列的最小代价. 这个是一个dp,对于这个dp的定义我觉得不是很好想,如果第一次碰到的话. ...

  7. 如何用python将一个时间序列转化成有监督学习

    机器学习可以被用于时间序列预测. 在机器学习能使用之前,时间序列预测需要被重新转化成有监督学习.将一个序列组合成成对的输入输出序列. 在这篇教程中,你会发现如何通过使用机器学习算法将单变量和多变量的时 ...

  8. BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】

    A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. O ...

  9. 最小正子序列(序列之和最小,同时满足和值要最小)(数据结构与算法分析——C语言描述第二章习题2.12第二问)

    #include "stdio.h" #include "stdlib.h" #define random(x) (rand()%x) void creat_a ...

随机推荐

  1. STS 新建mvc工程--helloworld

    File--New--Spring Template Project 选择Spring MVC Project 填写项目名称和基础的包名 Finish之后就完成了. 把项目添加到服务器,然后运行.在浏 ...

  2. c#调用Excel绘制图表

    c#调用Excel需要引用命名空间 using Microsoft.Office.Interop.Excel; 由于该程序不复杂,主要是根据不同数据画表和图,画的图像也并不复杂,因为画图和画表的操作会 ...

  3. Android系统进程间通信(IPC)机制Binder中的Client获得Server远程接口过程源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6633311 在上一篇文章中,我 们分析了And ...

  4. qt 操作excel表格

     自己编写的一个Qt C++类,用于操作excel表格,在Qt中操作excel需在.pro中增加CONFIG+=qaxcontainer配置. 1.打开Excel:objExcel = new QAx ...

  5. 谷歌推出情境感知API

    在 Google I/O 2016 大会上,我们宣布推出新的 Google Awareness API,让您的应用可以利用快照和围栏智能应对用户情境,并且仅需占用极少量的系统资源. 所有开发者均可以通 ...

  6. 适用于CSS2的各种运动的javascript运动框架

    <script> window.onload = function() { //var oDiv1 = document.getElementById('box1'); //var oDi ...

  7. 初学jquery遇见的两个小问题!

    <body>    <div id="divtest">div的内容</div>    <div id="default&quo ...

  8. NSString / NSMutableString 字符串处理,常用代码 (实例)

    http://blog.csdn.net/likendsl/article/details/7417878 Objective-C 中核心处理字符串的类是 NSString 与 NSMutableSt ...

  9. 简单的拖动手势控制侧拉view显示

    通过 UIPanGestureRecognizer  手势来控制侧拉view的显示 在QHLViewController.m文件中,先添加一些宏定义和参数等等. #define QHLAnimatin ...

  10. String 字符串相等比较