Codeforces Round 252 (Div. 2)
D
Valera and Swaps
Codeforces
2100
原题
← CE →
翻译中…

A permutation $*p*$ of length $*n*$ is a sequence of distinct integers $*p*_{1}, *p*_{2}, ..., *p*_{*n*}$ $(1 ≤ *p*_{*i*} ≤ *n*)$. A permutation is an identity permutation, if for any $*i*$ the following equation holds $*p*_{*i*} = *i*$.

翻译中…

A swap $(*i*, *j*)$ is the operation that swaps elements $*p*_{*i*}$ and $*p*_{*j*}$ 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 $*p*_{1}, *p*_{2}, ..., *p*_{*n*}$ ($1 ≤ *p*_{*i*} ≤ *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 $2*k*$ integers $*x*_{1}, *x*_{2}, ..., *x*_{2*k*}$ — the description of the swap sequence. The printed numbers show that you need to consecutively make swaps $(*x*_{1}, *x*_{2})$, $(*x*_{3}, *x*_{4})$, ..., $(*x*_{2*k* - 1}, *x*_{2*k*})$.

翻译中…

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

翻译中…

Note

翻译中…

Sequence $*x*_{1}, *x*_{2}, ..., *x*_{*s*}$ is lexicographically smaller than sequence $*y*_{1}, *y*_{2}, ..., *y*_{*s*}$, if there is such integer $*r*$ $(1 ≤ *r* ≤ *s*)$, that $*x*_{1} = *y*_{1}, *x*_{2} = *y*_{2}, ..., *x*_{*r* - 1} = *y*_{*r* - 1}$ and $*x*_{*r*} < *y*_{*r*}$.

翻译中…

样例

Input #1
5
1 2 3 4 5
2
Output #1
2
1 2 1 3
InputCopy OutputCopy
Input #2
5
2 1 4 5 3
2
Output #2
1
1 2
InputCopy OutputCopy
题目信息
题号
441D
时间限制
1 s
内存限制
256 MB
标签
构造
并查集
图论
模拟
数学
后缀结构

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。