CodeForces - 53D Physical Education

D. Physical Education
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and nis the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves.

Input

The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai(1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets.

Output

In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pipi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1.

Examples
input
4
1 2 3 2
3 2 1 2
output
4
2 3
1 2
3 4
2 3
input
2
1 100500
1 100500
output
0
题意:给出数字n和两个队列a,b,交换b的相邻两个数,使b队列等于a队列,输出交换次数和路径

思路:模拟

#include<bits/stdc++.h>
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
int main()
{
	int a[303],b[303];
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		int i,j;
		vector<int> v,s;
		for(i=1;i<=n;i++)
			scanf("%d",&a[i]);
		for(i=1;i<=n;i++)
			scanf("%d",&b[i]);
		
		for(i=1;i<=n;i++)
		{
			for(j=i;j<=n;j++)
			{
				if(a[i]==b[j])
					break;
			}
			while(j>i)
			{
				swap(b[j-1],b[j]);
				v.push_back(j-1);
				s.push_back(j);
				j--;
			}
		}
		printf("%d\n",v.size());
		for(i=0;i<v.size();i++)
		{
			printf("%d %d\n",v[i],s[i]);
		}
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值