题意:

定义 $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. php实现双色球算法

    function DoubleBall(){ $sysBlueball = mt_rand(1,16); $sysRedball = array(1,2,3,4,5,6,7,8,9,10,11,12, ...

  2. 【每日Scrum】第七天(4.28)Sprint2总结性会议

    本次会议主要是演示了一下本组项目的各项功能,每个人负责那一块儿功能由本人来负责说明和演示,确定alpha版本的发布时间,并且分派了各组员的文档负责情况,上图是会议记录,下面我详细介绍一下我组分派情况: ...

  3. QC3.0快充技术详解

    QC3.0 智能手机的电池容量愈来愈大,除了省电能力外,充电速度更成为用户愈来愈重视的特点.高通(Qualcomm)的 Quick Charge 快充技术已成为业界的典范之一,继 Quick Char ...

  4. 关于angular JS 中$timeOut 的一些不正常情况下的$destory

    最近项目中存在的问题头疼脑热了好一会. 我先简单说明下问题是由,使用$timeOut循环调用的时候由于页面存在异步加载会出现反复执行循环反复调用$timeOut,怎么清除跳出循环都不管用.于是查到了如 ...

  5. [转]JavaScript

    javascript 1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键 <table border on ...

  6. 在XP上安装VS2002

    在2002 年,随着 .NET 口号的提出与 Windows XP/Office XP 的公布,微软公布了 Visual Studio .NET(内部版本为 7.0). 使用VS2002+Object ...

  7. innodb的锁和高并发

    1 innodb的锁 1.1 s锁,即读锁,即share锁 1.2 x锁,即写锁,排他锁 1.3 s锁和x锁之间的关系 多个读锁可以共存,但是读锁不可以和写锁共存.写锁和写锁不可以共存. 1.4 间隙 ...

  8. PostgreSQL Client Authentication Configuration File

    PostgreSQL: Documentation: 10: 16.4. Installation Procedure https://www.postgresql.org/docs/10/stati ...

  9. yum 工作原理

    MySQL :: A Quick Guide to Using the MySQL Yum Repository https://dev.mysql.com/doc/mysql-yum-repo-qu ...

  10. Android笔记之获取应用程序文件目录

    获取应用程序文件目录 ContextWrapper.getFilesDir(),这个函数返回存放应用程序文件的目录的路径 获取应用程序缓存目录 ContextWrapper.getCacheDir() ...