Crazy Diamond CodeForces - 1148C(思维构造)

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
2
2 1
Output
1
1 2
Input
4
3 4 1 2
Output
4
1 4
1 4
1 3
2 4
Input
6
2 5 3 1 4 6
Output
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,2,3,,,n的序列,每一次交换需要这样做:对于i来说,和它交换的j需要满足2⋅|i−j|≥n.
思路:这个题一开始毫无思路,我知道给定的交换条件2⋅|i−j|≥n和最多不会超过5*n肯定是有原因的,但是没搞懂。。。看了题解之后明白了,对于一个想交换到另一半的数字,我们可以先与1或者n交换,然后再交换到相应的位置,这样的话,就能够保证交换的时候肯定是满足2⋅|i−j|≥n的了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=3e5+100;
int a[maxx];
int pos[maxx];
struct node{
	int x,y;
}p[maxx*5+100];
int n,cnt;

inline void judge(int x,int y)
{
	p[++cnt].x=x;p[cnt].y=y;
	swap(a[x],a[y]);
	pos[a[x]]=x,pos[a[y]]=y;
}
int main()
{
	scanf("%d",&n);cnt=0;
	for(int i=1;i<=n;i++) scanf("%d",&a[i]),pos[a[i]]=i;
	int i;
	for(i=2;i<=n/2;i++)
	{
		if(pos[i]<=n/2)
		{
			judge(pos[i],n);
			judge(n,i);
		}
		else
		{
			judge(pos[i],1);
			judge(1,n);
			judge(i,n);
		}
	}
	for(;i<n;i++)
	{
		if(pos[i]<=n/2)
		{
			judge(pos[i],n);
			judge(1,n);
			judge(1,i);
		}
		else
		{
			judge(pos[i],1);
			judge(1,i);
		}
	}
	if(a[1]!=1) judge(1,n);
	cout<<cnt<<endl;
	for(int i=1;i<=cnt;i++) cout<<p[i].x<<" "<<p[i].y<<endl;
}

努力加油a啊,(o)/~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值