题意:

  给定一个1~n的排列(n<=3000),输出字典序最小且次数最少的交换操作,使得操作后的排列可以通过最少m次交换得到排列[1,2,...n]

Solution:

  可以将排列的对应关系看做边,f[i]=i,代表自环。那么根据置换原理,图中有k个环,则需要最少n-k次交换操作得到排列[1,2...n]。所以,先找出图中的环,对同一个环的位置进行标记。这样对不在同一个环的两个位置进行交换,会将两个环合并。将在同一个环内的两个位置进行交换,会将这个环分成两个环。

只需要,判断需要加环还是去环。贪心选择序号较小的位置即可。

/*
置换群
*/
#include <bits/stdc++.h>
using namespace std; const int MAXN = ; int f[MAXN], g[MAXN], pos[MAXN]; int n, m, ans, sum, t;
int main() {
scanf ("%d", &n);
for (int i = ; i <= n; i++)
scanf ("%d", &g[i]), pos[g[i]] = i; scanf ("%d", &m); for (int i = ; i <= n; i++)
if (!f[i]) {
f[i] = ++sum;
for (int x = i; !f[g[x]]; x = g[x]) f[g[x]] = f[i];
} t = n - sum;
printf ("%d\n", m - t > ? m - t : t - m); for (int i = ; t < m; i++)
if (f[] != f[i]) {
t++;
for (int x = i; f[x] != f[]; x = g[x]) f[x] = f[];
printf ("1 %d ", i);
} for (int i = ; t > m; i++) {
if (g[i] != i)
for (int j = i + ; j <= n && t > m; j++)
if (f[i] == f[j]) {
printf ("%d %d ", i, j);
swap (g[i], g[j]);
t--;
if (g[i] == i) {
f[i] = -; break;
}
else
f[i] = f[g[i]]; if (g[j] == j) f[j] = -;
else {
f[j] = ++sum;
for (int x = j; f[g[x]] != sum; x = g[x]) f[g[x]] = sum;
}
}
}
return ;
}

Codeforces 441D Valera and Swaps(置换群)的更多相关文章

  1. CF(441D Valera and Swaps)置换群

    题意:1-n的一个排列, p2, ..., pn,f(p)的定义是此排列要交换最少的数对能够回到原排列1,2,3,4...n.给一个排列p.要将其变换成f值为m的排列,问至少要交换几个数对,并输出字典 ...

  2. [codeforces 339]E. Three Swaps

    [codeforces 339]E. Three Swaps 试题描述 Xenia the horse breeder has n (n > 1) horses that stand in a ...

  3. Codeforces 441C Valera and Tubes

    题目链接:Codeforces 441C Valera and Tubes 没看到r >= 2一直错.让前几个管子占用2个格子.最后一个把剩下的都占用了.假设问题有解.这样做一定有解.其它策略就 ...

  4. CodeForces - 369E Valera and Queries(树状数组)

    CodeForces - 369E Valera and Queries 题目大意:给出n个线段(线段的左端点和右端点坐标)和m个查询,每个查询有cnt个点,要求给出有多少条线段包含至少其中一个点. ...

  5. CodeForces - 441D: Valera and Swaps(置换群)

    A permutation p of length n is a sequence of distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n). A permu ...

  6. Educational Codeforces Round 14 D. Swaps in Permutation 并查集

    D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...

  7. codeforces 441B. Valera and Fruits 解题报告

    题目链接:http://codeforces.com/problemset/problem/441/B 题目意思:有 n 棵fruit trees,每课水果树有两个参数描述:水果成熟的时间和这棵树上水 ...

  8. Codeforces 425A Sereja and Swaps(暴力枚举)

    题目链接:A. Sereja and Swaps 题意:给定一个序列,能够交换k次,问交换完后的子序列最大值的最大值是多少 思路:暴力枚举每一个区间,然后每一个区间[l,r]之内的值先存在优先队列内, ...

  9. codeforces B. Valera and Contest 解题报告

    题目链接:http://codeforces.com/problemset/problem/369/B 题目意思:给出6个整数, n, k, l, r, sall, sk ,需要找出一个满足下列条件的 ...

随机推荐

  1. CnPack for delphi xe5

    CnPack Team is made up of Chinese Programmers and Delphi / C++ Builder fans across the Internet. Our ...

  2. Java获取当前路径

    1.利用System.getProperty()函数获取当前路径:System.out.println(System.getProperty("user.dir"));//user ...

  3. 排序Tip

    排序算法   所有排序算法汇总:http://en.wikipedia.org/wiki/Sort_algorithm counting sort 资料 :http://www.cs.miami.ed ...

  4. ssh互信自动化脚本(待更新)

    1.建立一个ip,端口,用户,密码列表 [root@localhost shell-key]# cat arg_list.txt 172.16.56.215 172.16.56.215 172.16. ...

  5. 南京Uber优步司机奖励政策(1月25日~1月31日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. JAVA学习.java.sql.date 与java.util.date以及gettime()方法的分析

    java.sql.Date 是针对SQL语句使用的,它只包含日期而没有时间部分. java.util.Date 就是在除了SQL语句的情况下面使用. 它都有getTime方法返回毫秒数,返回的是自19 ...

  7. sdut-2725-The Urge to Merge-状压DP

    把数组竖起来,从上往下走. 如果当前位置是竖着乘的,那么第一个点标记为1.否则标记为0. 样例最终的状态为: 0 0 1 0 1 0 1 0 0 0 0 0 #include<iostream& ...

  8. Python基础知识---字典

    现在在实习期间,好久没用Python了,今天在做Java项目时用的HashMap让我联想到了Python中的字典,就写一些Python字典的知识吧,复习复习. 字典:  key --> valu ...

  9. struts2学习笔记(2)---Action中訪问ServletAPI获取Map类型的Servlet元素

    源码: strust.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts ...

  10. zedboard--交叉编译Opencv库的生成 分类: shell ubuntu fool_tree的笔记本 ZedBoard OpenCV 2014-11-08 18:57 171人阅读 评论(0) 收藏

    Opencv的移植,xzyfeixiang和rainysky的博客. 第一步肯定是下载opencv的源码包 第二步已经做好的交叉编译环境. 第三步下载安装cmake   apt-get install ...