题意:

定义 $f(p)$ 表示将 $p$ 序列变换为有序序列最少要交换多少次,给一 $1 \sim n$ 的排列 $a$ ,给一整数 $m$,

求问将 $a$ 最少交换多少次能得到 $p$ ,使得 $f(p) = m$,并将以此交换视作一个两个数字,将交换按顺序排列开

求出字典序最小的交换序列。

解法:

记 $id$ 表示排列 $id(i) = i$

考虑 $f(p)$ 怎么求,可以发现,将原排列视作一个从$p$置换到$id$的置换,则将置换拆分成 $tot$ 个循环后,

最小交换次数就是$\sum_{i=1}^{tot}{cnt_i - 1}$,也就是$n - tot$。

这样考虑交换两个置换的元素,两个置换群会合为一个置换群, $tot$ 变为 $tot-1$。

考虑交换一个置换群内的元素,当前置换群会拆分为两个置换群,$tot$ 变为 $tot+1$。

我们注意到要求交换次数最小,这样两种操作一定不会共存,

这样分类讨论:

1.$n-m < tot$时,我们需要将原先的置换群不断合并,要求字典序最小,

所以我们每次找出含最小元素的置换,将其与含1的置换合并。

2.$n-m = tot$时,不用交换,答案为0

3.$n-m > tot$时,每一次我们只要选择含最小元素的置换,将其中的最小值和次小值交换,并将置换拆开。

总复杂度$O(n^2)$

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector> #define N 3010
#define LL long long using namespace std; int n,m,tot;
int a[N];
bool v[N];
vector<int> cir[N]; int main()
{
while(~scanf("%d",&n))
{
for(int i=;i<=n;i++) scanf("%d",&a[i]);
scanf("%d",&m);
m=n-m;
for(int i=;i<=n;i++) v[i]=;
tot=;
for(int i=;i<=n;i++)
{
if(v[i]) continue;
int tmp=i;
cir[++tot].clear();
while(!v[tmp])
{
v[tmp]=;
cir[tot].push_back(tmp);
tmp=a[tmp];
}
}
if(tot==m) cout<<<<endl;
else if(tot>m)
{
cout<<tot-m<<endl;
for(int Te=;Te<=tot-m;Te++)
{
int t=;
for(int i=;i<=tot;i++)
if(!cir[i].empty() && (!t || cir[i][]<cir[t][]))
t=i;
cout<<cir[][]<<' '<<cir[t][]<<' ';
for(int i=;i<(int)cir[t].size();i++)
cir[].push_back(cir[t][i]);
cir[t].clear();
}
}
else
{
int tott=tot;
cout<<m-tot<<endl;
for(int Te=;Te<=m-tot;Te++)
{
int t=;
for(int i=;i<=tott;i++)
if(cir[i].size()> && (!t || cir[i][]<cir[t][]))
t=i;
int pos=;
for(int i=;i<(int)cir[t].size();i++)
if(cir[t][i]<cir[t][pos])
pos=i;
cout<<cir[t][]<<' '<<cir[t][pos]<<' ';
swap(cir[t][],cir[t][pos]);
cir[++tott].clear();
int cnt=;
for(int i=pos;i<(int)cir[t].size();i++)
cir[tott].push_back(cir[t][i]),cnt++;
while(cnt--) cir[t].pop_back();
}
}
}
return ;
}

Valera and Swaps的更多相关文章

  1. 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 ...

  2. Codeforces 441D Valera and Swaps(置换群)

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

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

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

  4. Codeforces Round 252 (Div. 2)

    layout: post title: Codeforces Round 252 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  5. [codeforces 339]E. Three Swaps

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

  6. 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 ...

  7. uva331 - Mapping the Swaps

    Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entries in the ...

  8. UVA Mapping the Swaps

    题目例如以下: Mapping the Swaps  Sorting an array can be done by swapping certain pairs of adjacent entrie ...

  9. [Codeforces Round #237 (Div. 2)] A. Valera and X

    A. Valera and X time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

随机推荐

  1. python(2)- python程序的编写简单介绍

    一.语句和语法 # 注释 \ 转译回车,继续上一行,在一行语句较长的情况下可以使用其来切分成多行,因其可读性差所以不建议使用 : 将两个语句连接到一行,可读性差,不建议使用 : 将代码的头和体分开 语 ...

  2. Anyoffice -HTML5大赛 悦心(基于H5开发安卓音乐app)-项目总结

    项目在线演示地址:http://rose111.applinzi.com/ github 地址:欢迎star https://github.com/midoxinxin/YueX-Music 悦心,一 ...

  3. Android异步载入AsyncTask具体解释

    曾看见有人说过.认为非常有道理.分享一下:   技术分为术和道两种:   (1)具体做事的方法是术.   (2)做事的原理和原则是道. 近期项目发现个重大问题.结果打log跟踪查是AsyncTask导 ...

  4. ElasticSearch 分页检索

    在ElasticSearch的多索引和多类别里说到我们在集群中有14个文档匹配我们的(空)搜索语句.单数仅仅有10个文档在hits数组中.我们怎样看到其它文档? 和SQL使用LIMITkeyword返 ...

  5. Kristen Grauman

    http://www.cs.utexas.edu/~grauman/ CV         Publications         Code           Data        Short ...

  6. java 文件的写入和读取

    //写入操作 方法1 OutputStream f = new FileOutputStream("C:/j/j.txt"); f.write("aaaaaaa" ...

  7. React - S1

    资料: 1. https://developer.mozilla.org/zh-CN/docs/Web/JavaScript 进度: 教程 - 高级内容remaining; 参考remaining j ...

  8. secureCRT与vim配置

    折腾了一天,给服务器新装了centos系统,用crt连接,vim用着很不习惯. 修改配色,快捷键啥的都不怎么起效. 后来发现.crt里的会话选项-终端-仿真 里配成xtream,使用颜色方案就可以了.

  9. EasyDarwin开源社区 短视频拍摄项目Github地址

    在前面的几篇博客中,我们提到了EasyDarwin开源团队整理出来的短视频拍摄技术要点,这次我们将短视频技术及SDK整理成一个完整的Github项目进行长期维护,同时会支持安卓Android和IOS版 ...

  10. wcf系列(一)--- 寄宿方式

    一.自我寄宿(self-hosting) 1.wcf采用基于终结点(Endpoint)的通信手段:终结点由:地址(Address)+绑定(Binding)+契约(Contract)组成:  Enpoi ...