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

186 篇文章 0 订阅

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 and s of numbers from 1 to n. We can swap pi and pj, by paying |i - j| coins for it. Find and print the smallest number of coins required to obtain permutation s 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 from 1 to n — permutation p. Each number from 1 to n occurs exactly once in this line.

The third line contains a sequence of n numbers from 1 to n — permutation s. 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 permutation p 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 numbers i and j (1 ≤ i, j ≤ n, i ≠ j), which means that you need to swap pi and pj.

It is guaranteed that the solution exists.

Examples
Input
4
4 2 1 3
3 2 4 1
Output
3
2
4 3
3 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 permutation 3241 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值