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/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 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 贪心的更多相关文章
- 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 ...
- 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 ...
- 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know
题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...
- Codeforces Round #324 (Div. 2)解题报告
---恢复内容开始--- Codeforces Round #324 (Div. 2) Problem A 题目大意:给二个数n.t,求一个n位数能够被t整除,存在多组解时输出任意一组,不存在时输出“ ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- short s1 = 1; s1 = s1 + 1;有错而short s1 = 1; s1 += 1正确
这个问题以前碰到过,也研究过,发表一下: 如果你认为表达式(x += i)只是表达式(x = x + i)的简写方式,这并不准确.这两个表达式都被称为赋值表达式.第二个表达式使用的是简单赋值操作 ...
- eclipse 点击 open Implementation就退出eclipse
输入法不对.. 切换到纯英文的输入法. 微软自带的那个.. 我电脑上也这样. 现在好了 (安装谷歌输入法貌似存在这个问题)
- mysql 共享锁-排它锁
转 InnoDB 行级锁 http://www.cnblogs.com/dongqingswt/archive/2013/03/28/2987367.html InnoDB 行级锁 分类: 数据库2 ...
- quartz Cron表达式一分钟教程
CronTrigger CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔,复发的发射工作的时间表.CronTr ...
- LeetCode Balanced Binary Tree (判断平衡树)
题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1. 思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决. / ...
- 阿里云至 Windows Azure 的 Linux 虚拟机迁移
在Windows Azure中,用户可以对部署在Azure中的虚拟机的映像.磁盘以及快照进行生成和下载.用户可以方便地将Azure中的虚拟机实例迁移到本地.私有云甚至其他公有云平台进行测试.扩展或者再 ...
- TortoiseSVN中分支和合并实践
使用svn几年了,一直对分支和合并敬而远之,一来是因为分支的管理不该我操心,二来即使涉及到分支的管理,也不敢贸然使用合并功能,生怕合并出了问题对团队造成不良影响,最主要的原因是,自己对分支的目的和合并 ...
- (Java)利用ListIterator(iterator 重复器/迭代器的子接口) 操作ArrayList
add()方法是在下一个将要取得的元素之前插入新的元素.因此如果在下一个将要取得的元素的序号为0,则在序号0的元素前插入新的元素. 测试: 见第1.行,在序号为0的元素前添加一个元素. 见第2.行:这 ...
- 《深入Java虚拟机学习笔记》- 第8章 连接模型
Java虚拟机学习笔记(八)连接模型
- Action 操作
当鼠标移动到图片文件夹的时候,将有一些button显示 当鼠标移开这个文件夹,那些button隐藏了起来 display属性的变化 1.可以使用Js改变属性来操作 暂未验证,待时间. 2.可以使用Ac ...