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 ,需要找出一个满足下列条件的 ...
随机推荐
- mkimage使用详解
uboot源代码的tools/目录下有mkimage工具,这个工具可以用来制作不压缩或者压缩的多种可启动映象文件. mkimage在制作映象文件的时候,是在原来的可执行映象文件的前面加上一个0x40字 ...
- unity3d Realistic eye shading 真实的眼睛渲染
先放上效果 人皮都做了,当然要来研究下眼睛,眼睛要比人体皮肤简单一些(实时模拟人皮在此) 一看是不是很复杂 这是眼睛的解构,但是,我们只需要模拟出虹膜巩膜和角膜就能达到相当真实的眼睛 巩膜就是白眼球 ...
- java多线程编程(3)买票
1,买票非同步版本 http://www.cnblogs.com/anbylau2130/archive/2013/04/17/3025347.html很详细 public class 多线程2 { ...
- Broken line - SGU 124(判断点与多边形的关系)
题目大意:RT 分析:构造一条射线,如果穿越偶数条边,那么就在多边形外面,如果穿越奇数条边,那么就在多边形里面. 代码如下: ===================================== ...
- 用python 装饰器打log
# coding=utf-8 from time import time def logged(when): def log(f,*args,**kargs): prin ...
- [AngularJS] Default Child state and nav between child state
Let's say we want a parent state which is a abstract state. Two children states, one is for sinlge a ...
- Android开发实例-健康食谱应用(一)
转载请注明出处:http://blog.csdn.net/einarzhang/article/details/44774635 本系列文章的重点是如何使用Android开发一个简单的健康食谱软件.使 ...
- iOS 复杂动画之抽丝剥茧
一.前言 随着开发者的增多和时间的累积,AppStore已经有非常多的应用了,每年都有很多新的APP产生.但是我们手机上留存的应用有限,所以如何吸引用户,成为产品设计的一项重要内容.其中炫酷的动画效果 ...
- iOS 手机淘宝加入购物车动画分析
1.最终效果 仿淘宝动画 2.核心代码 _cartAnimView=[[UIImageView alloc] initWithFrame:CGRectMake(_propView.frame.size ...
- Android群英传》读书笔记 (4) 第八章 Activity和Activity调用栈分析 + 第九章 系统信息与安全机制 + 第十章 性能优化
第八章 Activity和Activity调用栈分析 1.Activity生命周期理解生命周期就是两张图:第一张图是回字型的生命周期图第二张图是金字塔型的生命周期图 注意点(1)从stopped状态重 ...