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的更多相关文章

  1. Codeforces Round 252 (Div. 2)

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

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

  3. codeforces Round #252 (Div. 2) C - Valera and Tubes

    贪心算法,每条路径最短2格,故前k-1步每次走2格,最后一步全走完 由于数据比较小,可以先打表 #include <iostream> #include <vector> #i ...

  4. Codeforces Round #252 (Div. 2) B. Valera and Fruits

    #include <iostream> #include <vector> #include <algorithm> #include <map> us ...

  5. Codeforces Round #252 (Div. 2) A - Valera and Antique Items

    水题 #include <iostream> #include <set> #include <vector> #include <algorithm> ...

  6. CodeForces 441E(Codeforces Round #252 (Div. 2))

    思路:dp[i][now][mark][len]   i 表示当前第i 次now存的是后8位,mark为第9位为0还是1 len第九位往高位还有几位和第9位相等.  只存后8位的原因:操作只有200次 ...

  7. Codeforces Round #252 (Div. 2) 441B. Valera and Fruits

    英语不好就是坑啊.这道题把我坑残了啊.5次WA一次被HACK.第二题得分就比第一题高10分啊. 以后一定要加强英语的学习,要不然就跪了. 题意:有一个果园里有非常多树,上面有非常多果实,为了不然成熟的 ...

  8. Codeforces Round #252 (Div. 2)-C,D

    C题就是一个简单的模拟.首先给每一个人两个.然后把剩下的都给一个人就好了. 给的时候蛇形给. #include<stdio.h> #include<string.h> #inc ...

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

随机推荐

  1. poj 2385 Apple Catching 基础dp

    Apple Catching   Description It is a little known fact that cows love apples. Farmer John has two ap ...

  2. Jmeter 分布式压力测试

      JMeter中进行分布式测试 作为一个纯 JAVA 的GUI应用,JMeter对于CPU和内存的消耗还是很惊人的,所以当需要模拟数以千计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心, ...

  3. Linux 常用的压缩命令有 gzip 和 zip

    Linux 常用的压缩命令有 gzip 和 zip,两种压缩包的结尾不同:zip 压缩的后文件是 *.zip ,而 gzip 压缩后的文件 *.gz 相应的解压缩命令则是 gunzip 和 unzip ...

  4. DataTable转化成实体对象

    /// <summary> /// The data extension. /// </summary> public static class DataExtension { ...

  5. Qt532_字符编码转换

    1.测试代码: // http://blog.csdn.net/changsheng230/article/details/6588447 QString str = QString::fromLoc ...

  6. 如何配置Smarty模板

    <?php //首先包含Smarty类文件 include_once('Smarty/Smarty.class.php'); //实例化Smarty类文件 $smarty=new Smarty( ...

  7. 在WinForm应用程序中嵌入WPF控件

    我们知道,在WPF界面上添加WinForm的控件需要使用WindowsFormHost类.而在WinForm界面上添加WPF控件该如何做呢?有没有类似的类呢?明显是有的,ElementHost就是为了 ...

  8. CentOS 7 Install Redis

    1. yum install epel-release 2. yum install –y redis 3. start : systemctl start redis.service 4. stat ...

  9. bzoj4589: Hard Nim fwt

    题意:求n个m以内的素数亦或起来为0的方案数 题解:fwt板子题,先预处理素数,把m以内素数加一遍(下标),然后fwt之后快速幂即可,在ifwt之后a[0]就是答案了 /*************** ...

  10. failed to load response data

    当需要根据后台传回地址跳转页面时 即使使用preserve log 可以查看上一个页面获取地址请求,但是此时请求返回值为failed to load response data 当关闭页面跳转可以查看 ...