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. Spark Streaming揭秘 Day27 Job产生机制

    Spark Streaming揭秘 Day27 Job产生机制 今天主要讨论一个问题,就是除了DStream action以外,还有什么地方可以产生Job,这会有助于了解Spark Streaming ...

  2. Perl中的正则表达

    前几天用到了Perl语言,主要看了一下Perl中的正则表达式,在各种网页语言中,正则表达式在处理字符串的时候十分有用,所以这里就简单说一下在Perl中正则表达式的应用. 先上代码 #!/usr/bin ...

  3. C# 将list<>泛型集合 转化为 DataTable

    使用案例:将页面easy ui 中datagrid表格中的数据,存成json字符串, 通过ajax和ashx传入C#将string类型的json字符串解析成list<>泛型集合, 由于业务 ...

  4. android应用activity中调出输入法后界面调整问题的解决

    在自己写的一个小应用中发现一个问题,当调出输入法后界面最下方的一个按钮被挤到了输入法的上面,这样很不美观,所以找了一下解决办法记录如下: 在AndroidManifest.xml文件中找到对应的act ...

  5. webx学习笔记

    Webx学习笔记周建旭 2014-08-01 Webx工作流程 图 3.2. Webx Framework如何响应请求 当Webx Framework接收到一个来自WEB的请求以后,实际上它主要做了两 ...

  6. iphone编程,使用代码结束程序

    (ios 4环境) 1.使用代码可以结束自己只有一种方式,而且apple不建议这样用,就是调用exit()函数.在2.0以前uiapplication类中有一个方法可以调用,但是现在已经没有了,而且如 ...

  7. 去掉url后面的#

    <a href="#" 这个代码,当你点击后会在url后面添加# 怎么去掉呢?解决方法:点击这里 <a href="javascript:void(0)&qu ...

  8. 简单3d RPG游戏 之 005 选择敌人

    选择一个敌人,按ctrl+d,复制出3个,调整一下它们的位置,不重叠,修改Tag为Enemy,禁用EnemyAI. 创建Targetting脚本,绑定到Player玩家对象 public class ...

  9. 实时数据处理环境搭建flume+kafka+storm:4.storm安装配置

    1.解压 apache-storm-0.9.3.tar.gz   2.修改配置文件 conf/storm.yaml --zk地址  storm.zookeeper.servers:  - " ...

  10. 分享SCI写作经验和一些工具