Codeforces 584E Anton and Ira【思维+贪心】好题~

来源:互联网 发布:上瘾网络剧微博 编辑:程序博客网 时间:2024/05/29 08:26

E. Anton and Ira
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Anton loves transforming one permutation into another one by swapping elements for money, and Ira doesn't like paying for stupid games. Help them obtain the required permutation by paying as little money as possible.

More formally, we have two permutations, p ands of numbers from 1 ton. We can swap pi andpj, by paying|i - j| coins for it. Find and print the smallest number of coins required to obtain permutations from permutation p. Also print the sequence of swap operations at which we obtain a solution.

Input

The first line contains a single number n (1 ≤ n ≤ 2000) — the length of the permutations.

The second line contains a sequence of n numbers from1 to n — permutationp. Each number from 1 to n occurs exactly once in this line.

The third line contains a sequence of n numbers from1 to n — permutations. Each number from 1 to n occurs once in this line.

Output

In the first line print the minimum number of coins that you need to spend to transform permutationp into permutation s.

In the second line print number k (0 ≤ k ≤ 2·106) — the number of operations needed to get the solution.

In the next k lines print the operations. Each line must contain two numbersi and j (1 ≤ i, j ≤ n,i ≠ j), which means that you need to swappi andpj.

It is guaranteed that the solution exists.

Examples
Input
44 2 1 33 2 4 1
Output
324 33 1
Note

In the first sample test we swap numbers on positions 3 and 4 and permutation p becomes 4 2 3 1. We pay |3 - 4| = 1 coins for that. On second turn we swap numbers on positions 1 and 3 and get permutation3241 equal to s. We pay|3 - 1| = 2 coins for that. In total we pay three coins.


题目大意:


给你长度为N的两个序列,上边序列表示当前序列,下边序列表示目标序列。

我们可以进行无限次操作,使得交换两个数的位子,使得当前序列变成目标序列。

现在交换位子i,j的两个数,要花费abs(i-j);

问最小花费的方式,使得当前序列变成目标序列。


思路:


1、序列中无非有三种数:

①不用动的

②想要往右动的

③想要往左动的

我们抛开①类数不谈,那么只剩下②③两种数。

很明显,如果我们交换了两个都想向右动的数,那么肯定要带来不必要的多余的操作,因为其中一个数肯定是向右动了,对于这个数来讲是距离自己想要到达的目标近了些,但是另外一个数向左动了,相当于让他距离他的目标位子更加远了。


2、那么根据上述简单的分析,显然我们每次交换一个想向右动的数和一个想向左动的数是最优的。

那么我们预处理出来所有数想要动的方向,暴力预处理即可,时间复杂度O(n^2)。

接下来同样暴力模拟,我们每一次找到最右边的那个想要向右动的数的位子,然后让他向右依次和想要向左边动的数进行交换,直到到达了目的地为止(这里保证一定是可以到达目的地的,因为这个数是序列最右边的那个想要向右动的数的位子,那么目标位子上边的那个数一定是想要往左动的,那么这里就能保证一定到达目的地)。

过程维护花费和操作情况即可。

时间复杂度O(n^2);


3、注意一些代码实现的细节别的就没有什么了。


Ac代码:


#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>using namespace std;struct node{    int x,y;}p[2050000];int dir[2005];//l = 1 r = 2;int a[2005];int b[2005];int main(){    int n;    while(~scanf("%d",&n))    {        for(int i=1;i<=n;i++)scanf("%d",&a[i]);        for(int i=1;i<=n;i++)scanf("%d",&b[i]);        for(int i=1;i<=n;i++)        {            if(a[i]==b[i])dir[i]=0;            else            {                for(int j=1;j<=n;j++)                {                    if(a[i]==b[j])                    {                        if(j<i)dir[i]=1;                        else dir[i]=2;                    }                }            }        }        int cnt=0;        int ans=0;        int pos=0;        for(int i=1;i<=n;i++)if(dir[i]==2)pos=i;        while(1)        {            for(int i=pos;i>=0;i--)            {                if(dir[i]==2)                {                    pos=i;                    break;                }            }            int now=pos;            for(int j=pos+1;j<=n;j++)            {                if(dir[j]==1)                {                    p[cnt].x=now;                    p[cnt].y=j;                    cnt++;                    swap(a[now],a[j]);                    swap(dir[now],dir[j]);                    ans+=abs(now-j);                    now=j;                }                if(a[now]==b[now])break;            }            pos--;            int flag=0;            for(int i=1;i<=n;i++)            {                if(a[i]==b[i])dir[i]=0;                if(dir[i]!=0)flag=1;            }            if(flag==0)break;        }        printf("%d\n",ans);        printf("%d\n",cnt);        for(int i=0;i<cnt;i++)        {            printf("%d %d\n",p[i].x,p[i].y);        }    }}








0 0
原创粉丝点击