Codeforces 441D Valera and Swaps(置换群)
题意:
给定一个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(置换群)的更多相关文章
- CF(441D Valera and Swaps)置换群
题意:1-n的一个排列, p2, ..., pn,f(p)的定义是此排列要交换最少的数对能够回到原排列1,2,3,4...n.给一个排列p.要将其变换成f值为m的排列,问至少要交换几个数对,并输出字典 ...
- [codeforces 339]E. Three Swaps
[codeforces 339]E. Three Swaps 试题描述 Xenia the horse breeder has n (n > 1) horses that stand in a ...
- Codeforces 441C Valera and Tubes
题目链接:Codeforces 441C Valera and Tubes 没看到r >= 2一直错.让前几个管子占用2个格子.最后一个把剩下的都占用了.假设问题有解.这样做一定有解.其它策略就 ...
- CodeForces - 369E Valera and Queries(树状数组)
CodeForces - 369E Valera and Queries 题目大意:给出n个线段(线段的左端点和右端点坐标)和m个查询,每个查询有cnt个点,要求给出有多少条线段包含至少其中一个点. ...
- 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 ...
- Educational Codeforces Round 14 D. Swaps in Permutation 并查集
D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...
- codeforces 441B. Valera and Fruits 解题报告
题目链接:http://codeforces.com/problemset/problem/441/B 题目意思:有 n 棵fruit trees,每课水果树有两个参数描述:水果成熟的时间和这棵树上水 ...
- Codeforces 425A Sereja and Swaps(暴力枚举)
题目链接:A. Sereja and Swaps 题意:给定一个序列,能够交换k次,问交换完后的子序列最大值的最大值是多少 思路:暴力枚举每一个区间,然后每一个区间[l,r]之内的值先存在优先队列内, ...
- codeforces B. Valera and Contest 解题报告
题目链接:http://codeforces.com/problemset/problem/369/B 题目意思:给出6个整数, n, k, l, r, sall, sk ,需要找出一个满足下列条件的 ...
随机推荐
- HDU-2149 Public Sale
http://acm.hdu.edu.cn/showproblem.php?pid=2149 巴什博奕(Bash Game): Public Sale Time Limit: 1000/1000 MS ...
- Linux学习笔记7——linux中的静态库和动态库
一.静态库的编译 静态库的编译过程如下: 1.编译成目标文件 这里有一个可选项-static,调用格式:gcc -c -static 代码文件名.c 2.归档成静态库 A.归档的工具是ar工具,使用a ...
- Aix项目_shell_rsh_01
rsh(remote shell) 功能说明:远端登入Shell. 语 法:rsh [-dn][-l <用户名称>][主机名称或IP地址][执行指令] 补充说明:rsh提供用户环境,也就是 ...
- sql2005中如何启用SA账号
如下图
- Error: rpmdb open failed
yumrpmdb: Thread/process 17132/140266190264064 failed: Thread died in Berkeley DB l ...
- mysql 表设计
- DOM方法
DOM常用方法: 方法 描述 getElementById() 返回带有指定 ID 的元素. getElementsByTagName() 返回包含带有指定标签名称的所有元素的节点列表(集合/节点数组 ...
- C#在ASP.NET4.5框架下的首次网页应用
运行效果预览: 先看实践应用要求: 1.编写一个函数,用于计算1!+2!+3!+4!+5!,在控制台或页面输出运行结果. 2.在控制台或页面输出九九乘法表. 3.输入10个以内的整数,输出该组整数的降 ...
- Dubbo xml配置 和注解配置 写法
<?xml version="1.0" encoding="UTF-8"?><!-- - Copyright 1999-2011 Alibab ...
- iOS开发总结-类似京东分类,UICollectionView
// // TypeViewController.m // BJ // // Created by shirenfeng on 16/11/6. // Copyright © 2016年 com.ws ...