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. Dapper.ColumnMapper 的使用

    using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; usin ...

  2. mvc 之 @Html.DropDownList

    Dictionary<string, string> myDic = new Dictionary<string, string>(); myDic.Add(System.DB ...

  3. DrawWindowFrame

    extern void DrawWindowFrame(HWND hWnd)//画窗口边框 { RECT rc; HWND DeskHwnd = ::GetDesktopWindow(); //取得桌 ...

  4. Demo学习: Dialogs Anonymous Callback

    Dialogs\Dialogs Anonymous Callback 窗体回调函数使用. 1. 标准回调函数 ShowMessage(const Msg: string; CallBack: TUni ...

  5. java 中的equal和"=="

    先看一段代码 String str1 = new String("str"); String str2 = new String("str"); System. ...

  6. OSI与TCP/IP模型之比较

    摘要:OSI参考模型和Internet模型(或称TCP/IP模型)作为计算网络发展过程影响力大的两大模型,它们共同之处是:都采用了层次结构的概念,从分析两者的异同入手,找出OSI的消亡和Interne ...

  7. 1054. The Dominant Color (20)

    时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Behind the scenes in the compute ...

  8. RecursiveDirectoryIterator目录操作类

    /** * @author Funsion Wu * @abstract SPL使用案例,全国首发,技术分享,欢迎转帖 */ class Dir extends RecursiveDirectoryI ...

  9. ORA-01034:oracle不可用 的解决方法

    晚上打开SQLPlus输入用户名和密码老是登不了,出现如上一行代码 "ORA-01034:ORACLE不可用"的错误 解决思路: 1.检查服务是否启动, 2.如果已经启动,说明数据 ...

  10. C#学习笔记---基础入门(二)

    枚举 枚举是被命名的整型常数的集合:枚举类型的变量只有赋值后才能使用:不同枚举中的枚举值可以重名:可以自定义枚举值. enum Playstates {            跑, 跳,下滑,左转,右 ...