A permutation p of length n is a sequence of distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n). A permutation is an identity permutation, if for any i the following equation holds pi = i.

A swap (i, j) is the operation that swaps elements pi and pj in the permutation. Let's assume that f(p) is the minimum number of swaps that you need to make the permutation p an identity permutation.

Valera wonders, how he can transform permutation p into any permutation q, such that f(q) = m, using the minimum number of swaps. Help him do that.

Input

The first line contains integer n (1 ≤ n ≤ 3000) — the length of permutation p. The second line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) — Valera's initial permutation. The last line contains integer m (0 ≤ m < n).

Output

In the first line, print integer k — the minimum number of swaps.

In the second line, print 2k integers x1, x2, ..., x2k — the description of the swap sequence. The printed numbers show that you need to consecutively make swaps (x1, x2), (x3, x4), ..., (x2k - 1, x2k).

If there are multiple sequence swaps of the minimum length, print the lexicographically minimum one.

Examples

Input
5
1 2 3 4 5
2
Output
2
1 2 1 3
Input
5
2 1 4 5 3
2
Output
1
1 2

Note

Sequence x1, x2, ..., xs is lexicographically smaller than sequence y1, y2, ..., ys, if there is such integer r (1 ≤ r ≤ s), that x1 = y1, x2 = y2, ..., xr - 1 = yr - 1 and xr < yr.

题意:有函数f(p),表示把排列p,变为顺序的排列,所用的最小交换次数,这里的交换是两两交换,而不是相邻交换。

现在给你排列p和m,让你交换最小的次数,使得排列变为q,满足f(q)=m,如果有多种交换方案,输出字典序最小的;

思路:1,把排列p变为1,2,3....p,的最小次数=N-环数。假如N-环数<m,那就加环。 否则减环。

2,如果交换两个不在同一个环的位置的数,那么环数+1;

3,如果交换在同一个环的位置的数,那么环数-1;

所以我们的任务就是加环或者减环,而每次分组的复杂度是O(N);我们最多加N次环,所以我们可以暴力交换,交换后重新分组。

复杂度O(N^2);

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int a[maxn],vis[maxn],tot,N,M;
void dfs(int u)
{
if(vis[u]) return ;
vis[u]=tot;
dfs(a[u]);
}
void rebuild()
{
tot=; rep(i,,N) vis[i]=;
rep(i,,N) if(!vis[i]) tot++,dfs(i);
}
int main()
{
scanf("%d",&N);
rep(i,,N) scanf("%d",&a[i]);
rebuild();
scanf("%d",&M);
if(M==N-tot) return puts(""),;
int ans,opt=;
if(M>N-tot) ans=M-N+tot,opt=; //合
else ans=N-tot-M;//拆
printf("%d\n",ans);
rep(i,,N)
rep(j,,N){
if(!ans) break;
if(i!=j&&(abs(vis[i]-vis[j])?:)==opt) {
swap(a[i],a[j]); ans--;
printf("%d %d ",i,j);
rebuild();
}
}
return ;
}

CodeForces - 441D: Valera and Swaps(置换群)的更多相关文章

  1. Codeforces 441D Valera and Swaps(置换群)

    题意: 给定一个1~n的排列(n<=3000),输出字典序最小且次数最少的交换操作,使得操作后的排列可以通过最少m次交换得到排列[1,2,...n] Solution: 可以将排列的对应关系看做 ...

  2. CF(441D Valera and Swaps)置换群

    题意:1-n的一个排列, p2, ..., pn,f(p)的定义是此排列要交换最少的数对能够回到原排列1,2,3,4...n.给一个排列p.要将其变换成f值为m的排列,问至少要交换几个数对,并输出字典 ...

  3. [codeforces 339]E. Three Swaps

    [codeforces 339]E. Three Swaps 试题描述 Xenia the horse breeder has n (n > 1) horses that stand in a ...

  4. Codeforces 441C Valera and Tubes

    题目链接:Codeforces 441C Valera and Tubes 没看到r >= 2一直错.让前几个管子占用2个格子.最后一个把剩下的都占用了.假设问题有解.这样做一定有解.其它策略就 ...

  5. CodeForces - 369E Valera and Queries(树状数组)

    CodeForces - 369E Valera and Queries 题目大意:给出n个线段(线段的左端点和右端点坐标)和m个查询,每个查询有cnt个点,要求给出有多少条线段包含至少其中一个点. ...

  6. Educational Codeforces Round 14 D. Swaps in Permutation 并查集

    D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...

  7. codeforces 441B. Valera and Fruits 解题报告

    题目链接:http://codeforces.com/problemset/problem/441/B 题目意思:有 n 棵fruit trees,每课水果树有两个参数描述:水果成熟的时间和这棵树上水 ...

  8. Codeforces 425A Sereja and Swaps(暴力枚举)

    题目链接:A. Sereja and Swaps 题意:给定一个序列,能够交换k次,问交换完后的子序列最大值的最大值是多少 思路:暴力枚举每一个区间,然后每一个区间[l,r]之内的值先存在优先队列内, ...

  9. codeforces B. Valera and Contest 解题报告

    题目链接:http://codeforces.com/problemset/problem/369/B 题目意思:给出6个整数, n, k, l, r, sall, sk ,需要找出一个满足下列条件的 ...

随机推荐

  1. Easyui的datagrid的行编辑器Editor中添加事件(修改某个单元格带出其他单元格的值)

    项目中有个datagrid需要编辑行时,用到Editor的属性,那么如何添加一个事件 问题:同一个编辑行中的某个单元格值改变时,修改其他单元格的值 页面用到的datagrid <table id ...

  2. pycharm(Tip of Day)

    You can easily override the methos of the base class by press Ctrl + 0 ( code | override methods) Yo ...

  3. mybatis输出sql语句

    方法一: 这种方法是mybatis官网上介绍的,比较好用: log4j.properties: log4j.rootLogger=ERROR,consolelog4j.appender.console ...

  4. [POJ3378]Crazy Thairs

    Problem 给你一个数列,让你求由五个元素组成的顺序对的个数. Solution DP:用DP[i][j]表示把第j个作为五元组中第i个的方案数 则DP[i][j]=sum{DP[k][j-1]} ...

  5. angular4-事件绑定

    事件绑定语法(可以通过 (事件名) 的语法,实现事件绑定) <date-picker (dateChanged)="statement()"></date-pic ...

  6. 一: Docker的概念

    附件:https://files.cnblogs.com/files/chaos-li/docker-k8s-devops-master-9287a2ca56433ca076078b564de9488 ...

  7. 十二. Python基础(12)--生成器

    十二. Python基础(12)--生成器 1 ● 可迭代对象(iterable) An object capable of returning its members one at a time. ...

  8. 接口测试-webservice接口---soapui

    1.添加项目 2.填入wsdl地址 3.编辑参数,运行接口

  9. C# struct and enum

    struct Person { public int age; public string name; public string fname; public string class; } enum ...

  10. day 67 django 之ORM 增删改查基础

    一 操作基础前提准备 1. 新建django 项目 mysite  子项目app01 ,选择好做路径. 2  .2-1在app01 下面models 中引用 模块 from  django.db im ...