Sorting by Swapping
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9514   Accepted: 5094

Description

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

Input

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.

Output

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.

Sample Input

2
3
1 2 3
5
2 3 5 4 1

Sample Output

0
3

Source

POJ Monthly--2004.06.27 弱人
 
置换群定义入门题目
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
#define N 10010 int n;
int a[N]; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
int cnt=;
for(int i=;i<=n;i++)
{
while(a[i]!=i) {
swap(a[i],a[a[i]]);
cnt++;
}
}
printf("%d\n",cnt);
}
return ;
}

[POJ 1674] Sorting by Swapping的更多相关文章

  1. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

  2. 拓扑排序 POJ 1049 Sorting It All Out

    题目传送门 /* 拓扑排序裸题:有三种情况: 1. 输入时发现与之前的矛盾,Inconsistency 2. 拓扑排序后,没有n个点(先判断cnt,即使一些点没有边连通,也应该是n,此时错误是有环): ...

  3. poj 1094 Sorting It All Out (拓扑排序)

    http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  4. POJ 1094 Sorting It All Out 拓扑排序 难度:0

    http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...

  5. poj.1094.Sorting It All Out(topo)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28762   Accepted: 99 ...

  6. poj 1094 Sorting It All Out(nyoj 349)

    点击打开链接 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24544   Accep ...

  7. poj 1094 Sorting It All Out(图论)

    http://poj.org/problem?id=1094 这一题,看了个大牛的解题报告,思路变得非常的清晰: 1,先利用floyd_warshall算法求出图的传递闭包 2,再判断是不是存在唯一的 ...

  8. POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39602   Accepted: 13 ...

  9. POJ 1486 Sorting Slides (KM)

    Sorting Slides Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2831   Accepted: 1076 De ...

随机推荐

  1. C++ 编写 CorelDRAW CPG 插件例子(2)—ClearFill

    这是另一个例子: 贴上主要代码: #include "stdafx.h" #include <tchar.h> #import "libid:95E23C91 ...

  2. PyQt4学习笔记2:事件和信号

    事件是任何 GUI 程序中很重要的部分.所有 GUI 应用都是事件驱动的.一个应用对其生命期产生的不同的事件类型做出反应.事件是主要由应用的用户产生.但是,也可以通过其他方法产生,比如,网络通信,窗口 ...

  3. MySQL --log-slave-updates

     官方说明:--log-slave-updates Command-Line Format --log-slave-updates Option-File Format log-slave-updat ...

  4. Unity3d之Http通讯GET方法和POST方法

    (一)GET方法 IEnumerator SendGet(string _url) { WWW getData = new WWW(_url); yield return getData; if(ge ...

  5. servlet的生命周期与工作原理、使用!

    概念: Servlet是一个java程序运行在服务器上,处理客户端请求并做粗响应的程序!Servlet是和平台无关的服务器组件,它运行在Servlet容器中,Servlet容器 负责servlet和客 ...

  6. 自己实现的android树控件,android TreeView

    1.开发原因 在项目中经常需要一个需要一个树状框架,这是非常常见的控件.不过可能是谷歌考虑到android是手机系统,界面宽度有限, 所以只提供了只有二级的ExpandableListView.虽然这 ...

  7. 为什么hibernate需要事务?

    Hibernate是对JDBC的轻量级对象封装, Hibernate本身是不具备事务处理功能的,Hibernate事务实际上是底层的JDBC事务的封装,或者是JTA事务的封装. Hibernate的J ...

  8. KafkaSpout分析:配置

    public KafkaSpout(SpoutConfig spoutConf) { _spoutConfig = spoutConf;} 基于0.93版本的Storm SpoutConfig继承自K ...

  9. fiddler 插件开发二

    本篇主要讲解Fildder插件开发中的涉及到的主要接口与类. 1.IFiddlerExtension 接口 如果要开发的自定义插件有UI界面,则需要实现IFiddlerExtension 接口.你程序 ...

  10. cocos2dx Sprite setBlendFunc 使用颜色混合:加算,减算

    说明: 图片的混色是游戏的特效的特破口,应用的好,可以大量减少图片的使用量,有专门的介绍资料... setBlendFunc  相关参数,有多种组合方式,需要经验的积累 http://blog.csd ...