CF(441D Valera and Swaps)置换群
题意:1-n的一个排列 p1, p2, ..., pn,f(p)的定义是此排列要交换最少的数对能够回到原排列1,2,3,4...n。给一个排列p。要将其变换成f值为m的排列,问至少要交换几个数对,并输出字典序最小的那组答案。
解法:处理出全部的置换群,求出环数k,此时f值为n-k。然后推断n-k和m的大小,分为两种操作
1、加环,这个是在随意元素个数大于1的环内交换随意两个数都能够做到加环
2、减环,交换随意两个环的随意两个元素。就能够做到将两个环连接起来
题目要求是输出字典序最小,那么就暴力搞。
代码:
/******************************************************
* author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>
//freopen ("in.txt" , "r" , stdin);
using namespace std; #define eps 1e-8
#define zero(_) (abs(_)<=eps)
const double pi=acos(-1.0);
typedef long long LL;
const int Max=3010;
const int INF=1000000007; vector<int> vec[Max];
int all=0;
int num[Max];
bool rem[Max];
int m;
int n;
int yinggai;
void make(int t)
{
if(rem[t])
return ;
rem[t]=1;
vec[all].push_back(t);
make(num[t]);
}
struct point
{
int p1,p2;
} points[Max];
bool operator<(point a,point b)
{
if(a.p1!=b.p1)
return a.p1<b.p1;
return a.p2<b.p2;
}
void solve()
{
all=0;
memset(rem,0,sizeof rem);
vec[0].clear();
for(int i=1; i<=n; i++)
{
if(!rem[i])
make(i),all++,vec[all].clear();
}
for(int i=0; i<all; i++)
sort(vec[i].begin(),vec[i].end());
}
int main()
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
scanf("%d",num+i);
solve();
yinggai=n-all;
//cout<<" "<<yinggai<<endl;
scanf("%d",&m);
cout<<abs(yinggai-m)<<endl;
if(m>yinggai)
{
vector<int> help;
for(int i=0; i<all; i++)
{
if(vec[i][0]!=1)
help.push_back(vec[i][0]);
}
sort(help.begin(),help.end());
bool b=1;
printf("1 %d",help[0]);
for(int i=1; i<m-yinggai; i++)
printf(" 1 %d",help[i]);
cout<<endl;
}
else if(m<yinggai)
{
int t=yinggai-m;
//cout<<"fdas";
for(int i=0;i<t;i++)
{
solve();
pair<int,int > p(Max,Max);
for(int i=0;i<all;i++)
{
if(vec[i].size()>1&&vec[i][0]<p.first)
{
p.first=vec[i][0];
p.second=vec[i][1];
}
}
swap(num[p.first],num[p.second]);
if(i==0)
printf("%d %d",p.first,p.second);
else
printf(" %d %d",p.first,p.second);
}
}
return 0;
}
CF(441D Valera and Swaps)置换群的更多相关文章
- Codeforces 441D Valera and Swaps(置换群)
题意: 给定一个1~n的排列(n<=3000),输出字典序最小且次数最少的交换操作,使得操作后的排列可以通过最少m次交换得到排列[1,2,...n] Solution: 可以将排列的对应关系看做 ...
- 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 ...
- CF 369C . Valera and Elections tree dfs 好题
C. Valera and Elections The city Valera lives in is going to hold elections to the city Parliament ...
- CF 441E Valera and Number
CF 441E Description 一共执行\(k\)次,每次有\(p\%\)把\(x * 2\),有\((100 - p)\%\)把\(x + 1\).问二进制下\(x\)末尾期望\(0\)的个 ...
- Valera and Swaps
题意: 定义 $f(p)$ 表示将 $p$ 序列变换为有序序列最少要交换多少次,给一 $1 \sim n$ 的排列 $a$ ,给一整数 $m$, 求问将 $a$ 最少交换多少次能得到 $p$ ,使得 ...
- cf E. Valera and Queries
http://codeforces.com/contest/369/problem/E 题意:输入n,m; n 代表有多少个线段,m代表有多少个询问点集.每一个询问输出这些点的集合所占的线段的个数. ...
- cf D. Valera and Fools
http://codeforces.com/contest/369/problem/D 标号最小的两个人会有四种状态:a活b活,a死b活,a活b死,a死b死:按照这四种状态dfs就可以求出最后的数量. ...
- cf C. Valera and Elections
http://codeforces.com/contest/369/problem/C 先见边,然后dfs,在回溯的过程中,如果在这个点之后有多条有问题的边,就不选这个点,如果没有而且连接这个点的边还 ...
- cf B. Valera and Contest
http://codeforces.com/contest/369/problem/B 先对k个处理,先处理sk%k个为sk/k+1,如果sk/k==0,k个数都为sk/k:对与剩下的数也按照同样的方 ...
随机推荐
- struts——拦截器
什么是拦截器 拦截器(Interceptor)是Struts 2的一个强有力的工具,有许多功能都是构建于它之上,如国际化(前两篇博客介绍过).转换器,校验等. 拦截器是动态拦截Action调用的对象. ...
- [Immutable.js] Differences between the Immutable.js Map() and List()
The Immutable.js Map() is analogous to a Javascript Object or Hash since it is comprised of key-valu ...
- 12款 jquery轮播插件
Nivo Slider 首先推荐的这款插件号称世界上最棒的图片轮播插件,有独立的 jQuery 插件和 WordPress 插件两个版本. 目前下载量已经突破 1,800,000 次!jQuery 独 ...
- SQL server sysobjects表说明
sysobjects 表 在数据库内创建的每个对象(约束.默认值.日志.规则.存储过程等)在表中占一行.只有在 tempdb 内,每个临时对象才在该表中占一行. sysobjects 表结构: 列名 ...
- SQL重复记录查询的几种方法(转)
1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 代码如下: select * from people ) 2.删除表中多余的重复记录,重复记录是根据单个字段(people ...
- 使用 Require.js 引用第三方框架时遇到的一些情况
使用 Require.js 引用第三方框架时遇到的一些情况 在使用Require.js解析依赖的时候,会出现以下几种情况: 程序中的依赖关系 当前程序 依赖于 B包, B包 依赖于 A包 A包与B包两 ...
- UVALive 4119 Always an integer (差分数列,模拟)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Always an integer Time Limit:3000MS M ...
- 使用dict和set
Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. d = {'Michael': 95, ...
- 高可用集群(HA)之Keeplived原理+配置过程
原理--> 通过vrrp协议,定义虚拟路由,在多个服务节点上进行转移. 通过节点优先级,将初始虚拟路由到优先级高的节点上,checker工作进程检测到主节点出问题时,则降低此节点优先级,从而实现 ...
- Django auth 登陆后页面跳转至/account/profile,修改跳转至其他页面
这几天在学习django,django功能很强大,自带的auth,基本可以满足用户注册登陆登出,简单的用户注册登陆系统使用django auth足矣.当然也不是拿来就能用的,需要自己写登陆页面的模板, ...