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. 解决 windows2012 下无法安装 sql2008R2

    Press the Windows logo key, type control panel, and then click the Control Panel icon. Note If you a ...

  2. Android开发中Eclipse里的智能提示设置

    今天开始学习一下Android开发,直接在Android Developers下载的一个开发工具包,然后再下了一个JDK,配置完环境变量等一系列的工作后环境就搭建好了,在新建好第一个Android项目 ...

  3. Eclipse编辑器基本设置

    1.添加行号 在边缘处右键 2.改字体 字体的一般配置 3.去掉拼写错误检查 4.Java代码风格 代码格式化 Ctrl + Shift + F 之后点击右边的New按钮,新建一个风格. 点击OK 上 ...

  4. PL/SQL数据导入导出浅谈(1)

    近来需要通过PL/SQL向Oracle中导数据,特此总结一下 试例表:test 字段:id;name;org; 1.直接复制粘贴(当数据量不是特别大的时候) 1)使用select * from tes ...

  5. Relay log read failure

    root@localhost > show slave status\G*************************** 1. row ************************** ...

  6. asp 回发的时候样式变化

    在一个按钮确定后弹出一个提示框,在提示框没有关闭时有时会发现页面的样式发生变化. 解决方法: 在DIV外增加,<table><tr><td align="lef ...

  7. shell echo打印换行的方法

    echo要支持同C语言一样的\转义功能,只需要加上参数-e,如下所示: [~]#echo "Hello world.\nHello sea" Hello world.\nHello ...

  8. phpstorm运行在浏览器中执行php文件报502错误

    原因是之前mac自带的php5.5版本被我升级到了5.6 通过phpinfo()查看到目前php5.6的安装目录 重新制定一些interpreter的路径 /usr/local/php5/bin 就可 ...

  9. MVC视图引擎

    1.视图引擎:把视图解析成浏览器可执行的html代码 2.aspx视图: <%=表达式%>: <% C#代码段 %>: 3.razor视图: @(表达式):@ViewData[ ...

  10. csharp excel interop programming

    string fileName = "c:\\a.xlsx"; var application = new Application(); application.Visible = ...