Valera and Swaps
题意:
定义 $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的更多相关文章
- 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 ...
- Codeforces 441D Valera and Swaps(置换群)
题意: 给定一个1~n的排列(n<=3000),输出字典序最小且次数最少的交换操作,使得操作后的排列可以通过最少m次交换得到排列[1,2,...n] Solution: 可以将排列的对应关系看做 ...
- CF(441D Valera and Swaps)置换群
题意:1-n的一个排列, p2, ..., pn,f(p)的定义是此排列要交换最少的数对能够回到原排列1,2,3,4...n.给一个排列p.要将其变换成f值为m的排列,问至少要交换几个数对,并输出字典 ...
- Codeforces Round 252 (Div. 2)
layout: post title: Codeforces Round 252 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- [codeforces 339]E. Three Swaps
[codeforces 339]E. Three Swaps 试题描述 Xenia the horse breeder has n (n > 1) horses that stand in a ...
- 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 ...
- uva331 - Mapping the Swaps
Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entries in the ...
- UVA Mapping the Swaps
题目例如以下: Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entrie ...
- [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 ...
随机推荐
- 读取配置文件(configparser,.ini文件)
使用configparser来读取配置信息config.ini 读取的信息(config.ini)如下: [baseconf]host=127.0.0.1port=3306user=rootpassw ...
- selenium之 chromedriver与chrome版本映射表(更新至v2.29)
最新的chromedriver与chrome的对应关系表: chromedriver版本 支持的Chrome版本 v2.37 v64-66 v2.36 v63-65 v2.35 v62-64 v2.3 ...
- C语言malloc
在子函数里面动态申请的内存不会自动被系统收回的,因为这些空间在堆里面,而不是栈,平常所说的不能返回指向栈的指针,比如在子函数里面定义一个字符指针,指向常量"hello"因为函数调用 ...
- GreenPlum 安装方法详解
一.安装环境准备 1.磁盘环境准备 磁盘分区典型配置如下: 文件系统 文件格式 大小 / ext3 50GB,Linux系统的根目录,所有的目录都挂在这个目录下面,建议大小为 ...
- SDWebImage学习
SDWebImage学习 SDWebImage版本是:'4.2.2' SDWebImage是iOS开发中常用的图片加载的库,能下载并缓存图片.这次就着重介绍SDWebImage的特色功能:下载与缓存. ...
- iOS中从零開始使用protobuf
让我们一起打开以下这个链接 https://github.com/alexeyxo/protobuf-objc 在github上有protobuf-objc,当中的readme能够教会我们安装prot ...
- EasyDarwin开源流媒体服务器实现RTSP直播同步输出MP4、RTMP、HLS的方案思路
背景 近期跟开源团队商量,想在EasyDarwin上继续做一些功能扩展,目前EasyDarwin开源流媒体服务器只能够实现高效的RTSP推流直播转发/分发功能,输入与输出都是RTSP/RTP流,不能够 ...
- asp.net mvc4 之Webapi之客户端或服务器端安全控制
一.WebAPI的工作方式 WebAPI的工作方式:HTTP的请求最先是被传递到HOST中的,如果WebAPI是被寄宿在IIS上的,这个HOST就是IIS上,HOST是没有能力也没有必 要进行请求的处 ...
- sam模板
SAM模板 struct SAM{ * ; struct node{ node*nxt[],*fail; int len; }; node*root;int cnt; node no[maxn]; n ...
- codeforces Codeforces Round #273 (Div. 2) 478B
B. Random Teams time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...