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次就好了;把所有的加上就好了;

代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define mem(x, y) memset(x, y, sizeof(x))
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = ;
int vis[MAXN];
struct Node{
int pos,v;
friend bool operator < (Node a, Node b){
if(a.v != b.v){
return a.v < b.v;
}
else return a.pos < b.pos;
}
};
Node dt[MAXN];
int main(){
int N, kase = , T;
scanf("%d", &T);
while(T--){
scanf("%d", &N);
for(int i = ; i <= N; i++){
scanf("%d", &dt[i].v);
dt[i].pos = i;
}
sort(dt + , dt + N + );
mem(vis, );
int ans = ;
for(int i = ; i <= N; i++){
if(!vis[i]){
int num = ;
int j = i;
while(!vis[j]){
vis[j] = ;
num++;
j = dt[j].pos;
}
ans += num - ;
}
}
printf("Case %d: %d\n", ++kase, ans);
}
return ;
}

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. dispatch_group_async

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. dispat ...

  2. Tcp通讯协议

    了解了Udp通讯协议之后,我们再认识一个常用的通讯协议:Tcp Tcp传输特点: --依赖于Socket和ServerSocket对象 --建立客户端和服务端 --建立连接后,通过Socket中的 I ...

  3. 页面动态数据的滚动效果——jquery滚动组件(vticker.js)

    <script language="javascript" src="lirms/Test/jquery-1.4.2.js"></script ...

  4. vue-resource插件使用

    本文的主要内容如下: 介绍vue-resource的特点 介绍vue-resource的基本使用方法 基于this.$http的增删查改示例 基于this.$resource的增删查改示例 基于int ...

  5. JS实现下拉框选中不同的项,对应显示不同的信息

    实现的效果如下图: 页面代码 下拉框: <select id="select3" name="select3" onchange="showli ...

  6. [转]CENTOS6 VNCSERVER安装

    标签:vncservercentos6.0 ssh隧道 vncviewer centos 休闲 职场 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律 ...

  7. UIAlertView与UIActionSheet

    1.UIAlertView(警告框) 1.1 创建警告框,设置样式 - (IBAction)alertView:(UIButton *)sender {//创建button按钮 //创建警告框的实例 ...

  8. Telnet运用and Sqlserver connection failed

    今天的工作中,需要远程访问服务器上的数据库.但是,连接错误,Error code is 1326.说句实话,关于SqlServer 不能远程访问这个问题,我遇到过N次.可是每次都不认真去研究到底是什么 ...

  9. Citrix 服务器虚拟化之十二 Xenserver灾难恢复

    Citrix 服务器虚拟化之十二 Xenserver灾难恢复 (环境有限实验无法测试,配置步骤摘取自官方文档) XenServer 灾难恢复的工作原理在存储库(SR)上还原从主(生产)环境复制到备份环 ...

  10. YUI Array 之some(检测|any)

    YUI原码 YUI someYArray.some = Lang._isNative(Native.some) ? function (array, fn, thisObj) { return Nat ...