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. SpringMVC中对Controller使用AOP

    转自http://usherlight.iteye.com/blog/1306111 正确配置spring aop,在controller中使用AOP 在controller中使用AOP的问题主要在于 ...

  2. I.MX6 wm8962 0-001a: DC servo timed out

    /******************************************************************************* * I.MX6 wm8962 0-00 ...

  3. 一致性hash算法 - consistent hashing

    consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出,目前在 cache 系统中应用越来越广泛: 1 ...

  4. CentOS 7 安装 mariaDB

    1.安装数据库 [root@localhost ~]# yum -y  install mariadb-server mariadb mariadb-devel 2.启动数据库[root@localh ...

  5. (十二)学习CSS之display属性

    参考:http://www.w3school.com.cn/cssref/pr_class_display.asp 浏览器支持 所有主流浏览器都支持 display 属性. 注释:如果规定了 !DOC ...

  6. HDU 4825-Xor Sum(trie)

    题意: 给你一组数,开始询问给一个数  求组中与该数异或值最大的数. 分析:根据异或的特点 要想得到的异或值最大 尽可能的让两个数的每位都相反 先把给定的一组数建树,数的最后一位对应的节点保存这个数的 ...

  7. [OFBiz]开发 一

    1.使用Eclipse3.7.1 + subclipse plugins 1.8.2(svn client)http://subclipse.tigris.org/servlets/ProjectDo ...

  8. NOIP2014 寻找道路

    2.寻找道路 (road.cpp/c/pas) [问题描述] 在有向图G中,每条边的长度均为1,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1.路径上的所有点的出边所指 ...

  9. Sort--冒泡排序

    冒泡排序 public class BubbleSort { public static void bubblesort(int[] a){ for(int i=0;i<a.length-1;i ...

  10. 【poj2478】Farey Sequence

    题意: 求前n项的欧拉函数之和 题解: 预处理出所有欧拉函数 赤裸裸的模版题- - 没什么好说的 代码: #include <cstdio> typedef long long ll; ; ...