CF--Crazy Diamond--思维

C. Crazy Diamond

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a permutation pp of integers from 11 to nn, where nn is an even number.

Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:

  • take two indices ii and jj such that 2⋅|i−j|≥n2⋅|i−j|≥n and swap pipi and pjpj.

There is no need to minimize the number of operations, however you should use no more than 5⋅n5⋅n operations. One can show that it is always possible to do that.

Input

The first line contains a single integer nn (2≤n≤3⋅1052≤n≤3⋅105, nn is even) — the length of the permutation.

The second line contains nn distinct integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n) — the given permutation.

Output

On the first line print mm (0≤m≤5⋅n0≤m≤5⋅n) — the number of swaps to perform.

Each of the following mm lines should contain integers ai,biai,bi (1≤ai,bi≤n1≤ai,bi≤n, |ai−bi|≥n2|ai−bi|≥n2) — the indices that should be swapped in the corresponding swap.

Note that there is no need to minimize the number of operations. We can show that an answer always exists.

Examples

input

Copy

2
2 1

output

Copy

1
1 2

input

Copy

4
3 4 1 2

output

Copy

4
1 4
1 4
1 3
2 4

input

Copy

6
2 5 3 1 4 6

output

Copy

3
1 5
2 5
1 4

Note

In the first example, when one swap elements on positions 11 and 22, the array becomes sorted.

In the second example, pay attention that there is no need to minimize number of swaps.

In the third example, after swapping elements on positions 11 and 55 the array becomes: [4,5,3,1,2,6][4,5,3,1,2,6]. After swapping elements on positions 22 and 55 the array becomes [4,2,3,1,5,6][4,2,3,1,5,6] and finally after swapping elements on positions 11 and 44 the array becomes sorted: [1,2,3,4,5,6][1,2,3,4,5,6].

 

给出1-n的n个数,各不相同。现在是逆序,求用多少次将它能排好序,且操作有限制,即交换位置的两个数位置之差不可小于n/2.

例如例子  6 5 3 4 2 1 处理1时,我们将它和1位置交换,然后1位置的数和n位置的数交换,然后把n位置和i=1位置交换,2同理。

处理3时,将其与n位置交换,n再和i=3位置交换。

总之就4种情况,分类讨论。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=300000+66;
const int mod=1e9+7;
int n,m;
int a[maxn];
int p[maxn];
vector<pair<int,int> > v;
void Swap(int x,int y)
{
    v.emplace_back(make_pair(x,y));
    swap(p[a[x]],p[a[y]]);
    swap(a[x],a[y]);
}
int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
        p[a[i]]=i;//记录位置
    }
    int i;
    for(i=2; i<=n/2; i++)
    {
        if(p[i]<=n/2)
        {
            Swap(p[i],n);
            Swap(i,n);
        }
        else
        {
            Swap(p[i],1);
            Swap(1,n);
            Swap(n,i);
        }
    }
    for(; i<n; i++)
    {
        if(p[i]<=n/2)
        {
            Swap(p[i],n);
            Swap(n,1);
            Swap(1,i);
        }
        else
        {
            Swap(1,p[i]);
            Swap(1,i);
        }
    }
    if(a[1]!=1)Swap(1,n);
    printf("%d\n",v.size());
    for(int i=0;i<v.size();i++)
    {
        printf("%d %d\n",v[i].first,v[i].second);
    }
    return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值