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. EDM排版table设置padding在ie7下bug

    今天搞EDM发现一个在ie7下很扯淡的bug,由于以前没遇到过,所以花了点时间来解决下. IE7下bug重现: <table cellpadding="0" cellspac ...

  2. 函数fseg_create_general

    申请一个段 /**********************************************************************//** Creates a new segm ...

  3. bzoj4511: [Usaco2016 Jan]Subsequences Summing to Sevens

    前缀和. 设f[i]为前缀和%7=i的第一个点.那么答案就是max(i-f[s[i]%7])了. #include<cstdio> #include<algorithm> #i ...

  4. codevs3731 寻找道路

    方向dfs判定是否可行,spfa跑最短路. noip水题,wa好几次. #include<cstdio> #include<algorithm> #include<cst ...

  5. UVa 1641 ASCII Area

    题意: 就是用一个字符矩阵代表一个闭合的阴影部分,然后求阴影部分的面积. 分析: 一个'/'和'\'字符都代表半个小方块的面积. 关键就是判断'.'是否属于阴影部分,这才是本题的关键. 从第一列开始, ...

  6. SCOI2010游戏

    发现这题的并查集做法真是惊呆了 不过似乎匹配跑得更快? 对于一个联通块,假如不含环(就是一棵树),那么必定可以满足其中任意的p-1个点. 对于一个联通块,假如含环,那么必定全部的p个点都能满足. 那么 ...

  7. [转] struts.xml配置详解

    转自:http://www.cnblogs.com/fmricky/archive/2010/05/20/1740479.html struts.xml是我们在开发中利用率最高的文件,也是Struts ...

  8. I.MX6 Android i2c-tools porting

    /************************************************************************** * I.MX6 Android i2c-tool ...

  9. eayui 验证扩展

    $.extend($.fn.validatebox.defaults.rules, { idcard : {// 验证身份证 validator : function(value) { return ...

  10. 剑指Offer:从第一个字符串中删除第二个字符串中出现过的所有字符

    // 从第一个字符串中删除第二个字符串中出现过的所有字符 #include <stdio.h> char* remove_second_from_first( char *first, c ...