Codeforces Round #252 (Div. 2) D
http://codeforces.com/problemset/problem/441/D
置换群的基本问题,一个轮换内交换成正常顺序需要k-1次,k为轮换内元素个数
两个轮换之间交换元素,可以把两个轮换合并成1个,总交换次数+1
一个轮换内部交换,可以把一个轮换拆分成两个,总交换次数-1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std ;
int n,m ;
int nxt[],vis[] ;
vector <pair<int,int> > ans ;
int cal()
{
memset(vis,,sizeof(vis)) ;
int st= ;
int cnt= ;
for(int i= ;i<=n ;i++)
if(!vis[i])
{
vis[i]=st++ ;
int res= ;
for(int j=nxt[i] ;!vis[j] ;j=nxt[j])
{
vis[j]=vis[i] ;
res++ ;
}
cnt+=(res-) ;
}
return cnt ;
}
int main()
{
scanf("%d",&n) ;
for(int i= ;i<=n ;i++)
scanf("%d",&nxt[i]) ;
scanf("%d",&m) ;
while()
{
int ret=cal() ;
if(ret==m)break ;
if(ret<m)//两个轮换换,可以把两个轮换合并成1个,ret+1 ,保证字典序最小,所以和位置1换
{
for(int i= ;i<=n ;i++)
if(vis[i]!=vis[])
{
swap(nxt[],nxt[i]) ;
ans.push_back(make_pair(,i)) ;
break ;
}
}
else//一个轮换内换,可以把一个轮换拆成两个,ret-1
{
int i,j ;
for(i= ;i<=n ;i++)
if(nxt[i]!=i)break ;
for(int j=i+ ;j<=n ;j++)
if(vis[j]==vis[i])
{
swap(nxt[i],nxt[j]) ;
ans.push_back(make_pair(i,j)) ;
break ;
}
}
}
printf("%d\n",ans.size()) ;
for(int i= ;i<ans.size() ;i++)
printf("%d %d ",ans[i].first,ans[i].second) ;
return ;
}
Codeforces Round #252 (Div. 2) D的更多相关文章
- Codeforces Round 252 (Div. 2)
layout: post title: Codeforces Round 252 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- Codeforces Round #252 (Div. 2) B. Valera and Fruits(模拟)
B. Valera and Fruits time limit per test 1 second memory limit per test 256 megabytes input standard ...
- codeforces Round #252 (Div. 2) C - Valera and Tubes
贪心算法,每条路径最短2格,故前k-1步每次走2格,最后一步全走完 由于数据比较小,可以先打表 #include <iostream> #include <vector> #i ...
- Codeforces Round #252 (Div. 2) B. Valera and Fruits
#include <iostream> #include <vector> #include <algorithm> #include <map> us ...
- Codeforces Round #252 (Div. 2) A - Valera and Antique Items
水题 #include <iostream> #include <set> #include <vector> #include <algorithm> ...
- CodeForces 441E(Codeforces Round #252 (Div. 2))
思路:dp[i][now][mark][len] i 表示当前第i 次now存的是后8位,mark为第9位为0还是1 len第九位往高位还有几位和第9位相等. 只存后8位的原因:操作只有200次 ...
- Codeforces Round #252 (Div. 2) 441B. Valera and Fruits
英语不好就是坑啊.这道题把我坑残了啊.5次WA一次被HACK.第二题得分就比第一题高10分啊. 以后一定要加强英语的学习,要不然就跪了. 题意:有一个果园里有非常多树,上面有非常多果实,为了不然成熟的 ...
- Codeforces Round #252 (Div. 2)-C,D
C题就是一个简单的模拟.首先给每一个人两个.然后把剩下的都给一个人就好了. 给的时候蛇形给. #include<stdio.h> #include<string.h> #inc ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
随机推荐
- Codeforces Beta Round #16 div 2 C.Monitor最大公约数
C. Monitor time limit per test 0.5 second memory limit per test 64 megabytes input standard input ou ...
- vue-cli 组件运用
// components ----- helloworld.vue <script> export default { name: 'Hellowworld', props: { //接 ...
- Could NOT find SDL (missing: SDL_LIBRARY SDL_INCLUDE_DIR)
sudo apt-get install libsdl-dev或 sudo apt-get install libsdl1.2-dev
- netcat使用指南
在网络工具中有“瑞士军刀”美誉的NetCat, 在我们用了N年了至今仍是爱不释手.因为它短小精悍(这个用在它身上很适合,现在有人已经将其修改成大约10K左右,而且功能不减少).现在就我的一些使用心得和 ...
- Redis<一> 数据结构:String
). set key value : 将字符串值 value 关联到 key .如果 key 已经持有其他值, SET 就覆写旧值,无视类型. ). get key : 返回 key 所关联的字符串值 ...
- windows批处理命令
前言 批处理文件(batch file)包含一系列 DOS命令,通常用于自动执行重复性任务.用户只需双击批处理文件便可执行任务,而无需重复输入相同指令.编写批处理文件非常简单,但难点在于确保一切按顺序 ...
- 【异常】Caused by: java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around
原因:缺少aspect,AOP的maven坐标 导入maven坐标: <dependency> <groupId>org.springframework</groupId ...
- 源代码方式调试Mycat
如果是第一次刚接触MyCat建议下载源码在本地通过eclipse等工具进行配置和运行,便于深入了解和调试程序运行逻辑. 1)源代码方式调试与配置 由于MyCat源代码目前主要托管在github上,大家 ...
- Windows 平台安装 MongoDB
官方文档:https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/ 系统环境:win7 MongoDb:mongodb- ...
- golang martini 源码阅读笔记之inject
martini是go语言写的一个超级轻量的web开源框架,具体源码可在github搜索找到.13年那会开始接触go语言时有稍微看过这个框架,由于之后没有继续使用go就慢慢忽略了,最近由于手头项目可能会 ...