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. iBatis的基本配置+CRUD操作

    首先解释一下CRUD的含义:CRUD是指在做计算处理时的增加(Create).查询(Retrieve)(重新得到数据).更新(Update)和删除(Delete) 基本的数据库操作 创建工程iBati ...

  2. 新开发项目Jacoco代码覆盖率

    一般只有新的项目才会去用JaCoCo工具看一下代码覆盖率, 一来看看测试有没有漏的测试用例 二来看看开发有没有留下冗余的代码 新开发项目Jacoco代码覆盖率后端接口打成jar包,进行启动 #exec ...

  3. Codeforces 101628A - Arthur's Language

    101628A - Arthur's Language 思路:dp,状态转移见代码. 代码: #include<bits/stdc++.h> using namespace std; #d ...

  4. Codeforces 757B - Bash's Big Day(分解因子+hashing)

    757B - Bash's Big Day 思路:筛法.将所有因子个数求出,答案就是最大的因子个数,注意全为1的特殊情况. 代码: #include<bits/stdc++.h> usin ...

  5. 20170707xlVBA多区域拆分多表保持行高列宽

    Public Sub 多个区域拆分到多表() AppSettings On Error GoTo ErrHandler Dim StartTime, UsedTime As Variant Start ...

  6. codeforces 930b//Game with String// Codeforces Round #468 (Div. 1)

    题意:一个串,右循环移位后,告诉你第一个字母,还能告诉你一个,问你能确定移位后的串的概率. 用map记录每个字母出现的位置.对于每个字母,用arr[j][k]记录它的所有出现位置的后j位是字母k的个数 ...

  7. mysql查询出来的某一列合并成一个字段

    查询所有用户及角色,一个用户会有多个角色 select u.*,sr.* from users u left join sys_user_role sur on sur.userCode=u.id l ...

  8. 『PyTorch』第十弹_循环神经网络

    RNN基础: 『cs231n』作业3问题1选讲_通过代码理解RNN&图像标注训练 TensorFlow RNN: 『TensotFlow』基础RNN网络分类问题 『TensotFlow』基础R ...

  9. CF808D STL

    D. Array Division time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  10. 计时(.NET)

    using System.Diagnostics; // 开始计时 Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 要计时的操 ...