E. Anton and Ira

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/584/problem/E

Description

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.

Sample Input

4
4 2 1 3
3 2 4 1

Sample Output

3
2
4 3
3 1

HINT

题意

给你两个数组,你要求让第一个数组变成第二个数组,你可以进行交换操作

每次交换操作的代价是abs(i-j)

然后让你把方案输出出来

题解:

贪心题,你其实可以把每次交换想成线段一样的东西

你只要把这些线段压成一条线段就好了

------->
     <-------------
----------------->
----------->
  <---------------

直接暴力交换那些和自己有重叠的线段端点就好了

代码:

#include<iostream>
#include<math.h>
#include<string.h>
#include<vector>
#include<queue>
#include<stdio.h>
using namespace std; int a[];
int b[];
int c[];
int d[];
int ans1[];
int ans2[];
int main()
{
int n;
int tot = ;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
scanf("%d",&b[i]),c[b[i]]=i;
int ans= ;
for(int i=;i<=n;i++)
{
if(a[i]!=b[i]){
int k = -;
for(int j=i+;j<=n;j++)
if(a[j]==b[i])
{
k = j;
break;
}
while(k!=i)
{
for(int j = i;j<k;j++)
{
if(c[a[j]]>=k)
{
ans += fabs(k-j);
swap(k,j);
swap(a[k],a[j]);
ans1[tot]=k;
ans2[tot]=j;
tot++;
break;
}
}
}
}
}
cout<<ans<<endl;
cout<<tot<<endl;
for(int i=;i<tot;i++)
printf("%d %d\n",ans1[i],ans2[i]);
}

Codeforces Round #324 (Div. 2) E. Anton and Ira 贪心的更多相关文章

  1. Codeforces Round #329 (Div. 2)B. Anton and Lines 贪心

    B. Anton and Lines   The teacher gave Anton a large geometry homework, but he didn't do it (as usual ...

  2. Codeforces Round #324 (Div. 2) C. Marina and Vasya 贪心

    C. Marina and Vasya Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/pr ...

  3. 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know

    题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...

  4. Codeforces Round #324 (Div. 2)解题报告

    ---恢复内容开始--- Codeforces Round #324 (Div. 2) Problem A 题目大意:给二个数n.t,求一个n位数能够被t整除,存在多组解时输出任意一组,不存在时输出“ ...

  5. Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心

    Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  6. Codeforces Round #329 (Div. 2) B. Anton and Lines 逆序对

    B. Anton and Lines Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/pr ...

  7. Codeforces Round #404 (Div. 2) D. Anton and School - 2 数学

    D. Anton and School - 2 题目连接: http://codeforces.com/contest/785/problem/D Description As you probabl ...

  8. Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径

    E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...

  9. Codeforces Round #379 (Div. 2) D. Anton and Chess 水题

    D. Anton and Chess 题目连接: http://codeforces.com/contest/734/problem/D Description Anton likes to play ...

随机推荐

  1. VMWare-NAT模式实现局域网其他主机对虚拟机访问

    WIN 2012在桥接模式下可以实现主机及主机所在局域网内其他主机对虚拟机的访问,但是在NAT模式下主机可以对虚拟机访问,但是主机所在的局域网内其他主机却无法对虚拟机访问,必须进行主机转发,从而实现局 ...

  2. ORA-14452: 试图创建, 更改或删除正在使用的临时表中的索引

    ORA-14452: 试图创建, 更改或删除正在使用的临时表中的索引       因为表KOL_XX_FIN050_TEMP 为临时表,而且有其他session正在使用. 处理步骤: 1.先从 dba ...

  3. 宏ut_2pow_remainder

    求余数 12%8=4 n%m也能计算出余数,但效率可能比位操作要低一些 /*************************************************************// ...

  4. 结构体UT_LIST_ADD_LAST

    使用 UT_LIST_ADD_LAST(list, buf_pool->free, (&block->page)); /****************************** ...

  5. bzoj3774

    这算是最小割中比较难的吧 看到选取显然最小割 看到上下左右四个点我感觉肯定和染色相关 注意每个点的收益获得条件是[或],因此我们考虑拆点i', i,分别表示通过四周控制和控制本身的代价 连边s--&g ...

  6. compass和sass很好的两篇文章

    Sass是一种"CSS预处理器",可以让CSS的开发变得简单和可维护.但是,只有搭配Compass,它才能显出真正的威力. 本文介绍Compass的用法.毫不夸张地说,学会了Com ...

  7. HHVM 是如何提升 PHP 性能的?

    背景 HHVM 是 Facebook 开发的高性能 PHP 虚拟机,宣称比官方的快9倍,我很好奇,于是抽空简单了解了一下,并整理出这篇文章,希望能回答清楚两方面的问题: HHVM 到底靠谱么?是否可以 ...

  8. SPOJ-OPTM Optimal Marks ★★(按位建图 && 最小割)

    [题意]给出一个无向图,每个点有一个标号mark[i],不同点可能有相同的标号.对于一条边(u, v),它的权值定义为mark[u] xor mark[v].现在一些点的标号已定,请决定剩下点的标号, ...

  9. FirstOrDefault

    FirstOrDefault:取序列中满足条件的第一个元素,如果没有元素满足条件,则返回默认值(对于可以为null的对象,默认值为null,对于不能为null的对象,如int,默认值为0)

  10. 32、handler更新控件值

    import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os. ...