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

题意:给定两个排列p1和p2。可以交换p1中的两个元素p1[i],p1[j],花费为|i-j|,求最小的话费使得p1变为p2。

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 ≤ ni ≠ j), which means that you need to swap pi and pj.

It is guaranteed that the solution exists.

Sample test(s)
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.

分析:记p1[i]在p2中的位置为pos1,p1[j]在p2中的位置为pos2,要把p1[i]放到pos1位置,很明显,当pos1<=j而且po2<=i时p1[i]与p2[j]交换的性价比最高。然后3层循环枚举,一直交换,直到某个时刻数组扫了一遍没有元素交换,那么p1就变成了p2。

ps:居然过了。。。

代码:

#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
const int maxn = 3000;
int a[maxn],b[maxn];int mp[3000000];
struct node
{int x,y;node()=default;node(int a,int b):x(a),y(b){}
}s[3000000];
int Abs(int x)
{if(x<0)return -x;return x;
}
int main()
{int n,i,j,p=0,ans=0;cin>>n;for(i=1;i<=n;i++)cin>>a[i];for(i=1;i<=n;i++){cin>>b[i]; mp[b[i]]=i;}int cur,temp;while(true){int fg2=0;for(i=1;i<=n;i++){temp=mp[a[i]];if(temp==i)continue ;int cur=i,goal=temp;while(cur<goal){int fg1=0;for(j=cur+1;j<=n && j<=goal;j++){temp=mp[a[j]];if(temp<=cur){ans+=Abs(cur-j);s[p++]=node(cur,j);swap(a[cur],a[j]);cur=j;fg2=fg1=1;break;}}if(!fg1)break;}}if(!fg2)break;}printf("%d\n",ans);printf("%d\n",p);for(i=0;i<p;i++)printf("%d %d\n",s[i].x,s[i].y);return 0;
}

codeforces 324# E. Anton and Ira (暴力枚举+贪心)相关推荐

  1. 51nod 1574 || Codeforces 584 E. Anton and Ira 思维+构造+贪心

    传送门:E. Anton and Ira 题意:给定两个1-n的全排列p和s,假设交换pi和pj的代价是abs(i-j),问怎样将p交换成s才能使代价最小. 思路:先将s映射成1-n的顺序排列,再将p ...

  2. CodeForces - 253C:Text Editor(暴力枚举)

    Discription Vasya is pressing the keys on the keyboard reluctantly, squeezing out his ideas on the c ...

  3. codeforces 584 E. Anton and Ira(贪心,数组p经变换到数组s)

    题目:http://codeforces.com/contest/584/problem/E 题意:有两个数组p,s,长度为n,且是1~n的排列.要使数组p变为s,每交换 i 和 j 两个位子上的数, ...

  4. Codeforces Round #324 (Div. 2) E. Anton and Ira 贪心

    E. Anton and Ira Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/probl ...

  5. codeforces#324(div2) E. Anton and Ira 贪心

    codeforces#324(div2) E. Anton and Ira  贪心 E. Anton and Ira time limit per test 1 second memory limit ...

  6. codeforces数学1600day6[CodeForces - 1029C多区间交+枚举,CodeForces 992C[数学公式推导],CodeForces 992B[质因数分解+暴力枚举]]

    A - Maximal Intersection CodeForces - 1029C 题目大意:就是给你n个区间,这n个区间有公共的区间长度为x,现在叫你从这n个区间中删掉一个使得x最大化. 解题思 ...

  7. Codeforces 584E. Anton and Ira (排列好题)

    大致题意: n <= 2000,有一1-n的排列p和s,对pi和pj交换产生的代价是 | i - j | , 问最少需要多少代价使,排列p变成排列s,输出解 思路: 可以先把s映射成1...n的 ...

  8. Codeforces 846 A Curriculum Vitae 思维 暴力

    题目链接: http://codeforces.com/contest/846/problem/A 题目描述: 给你一个串, 你可以做删除操作, 要求结果串0不能在1的右边, 问最多可以剩几个数字 解 ...

  9. Codeforces 785 D.Anton and School - 2(组合数处理)

    Codeforces 785 D.Anton and School - 2 题目大意:从一串由"(",")"组成的字符串中,找出有多少个子序列满足:序列长度为偶 ...

最新文章

  1. 了解 C# “.NET研究”4 中的 Dynamic 关键字
  2. 005_JavaScript使用
  3. 2020 中国技术力量年度榜单
  4. 【机器学习】从电影数据集到推荐系统
  5. Chrome上出现的问题
  6. python展开 c函数中的宏预处理_C 语言常用的预处理-宏函数
  7. 电影票房数据查询服务高性能与高可用实践
  8. flutter网络请求dio的get、post、上传文件、下载文件总结
  9. c++11 string转ing_pdfkit | 利用python实现html文件转pdf
  10. python写一个完整的小程序_写一个python小程序
  11. Effective Go中文版(更新中)
  12. ASP.NET- 执行SQL超时的解决方案
  13. (Kinetis K60) FTM输出PWM
  14. jad反编译成java_反编译工具jad的使用(将*.class文件变成*.java文件,附带jad.zip包)[转]...
  15. Python自动登录QQ空间
  16. python就业班-淘宝-目录.txt
  17. 流数据分析之地理围栏应用
  18. 李宏毅老师《机器学习》课程笔记-2.1模型训练技巧
  19. EMC测试的目的是什么?
  20. 最速下降法(适用于求二阶极小值)

热门文章

  1. IntelliJ IDEA插件安装最全详解
  2. 316页11万字AI赋能智慧水利大数据信息化平台建设和运营解决方案
  3. 在Ubuntu上安装docker
  4. Kali 2.0 安装与基础配置
  5. WPF 实现蒙板控件
  6. 中国科学家成功筛选拉沙病毒入侵抑制剂
  7. 分享一下身边朋友自学android开发及找工作的那些事!【不足勿喷】 1
  8. 电商抖音直播公司KPI绩效工作运营计划表
  9. 《JavaScript编程精解》--读书笔记
  10. frameset的基础使用方法