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 ...
随机推荐
- Java面试题-Java中的锁
1. 如何实现乐观锁(CAS)?如何避免ABA问题? 答:1)读取内存值的方式实现了乐观锁(比如:SVN系统),方法:第一,比较内存值和期望值:第二,替换内存值为要替换值. 2)带参数版 ...
- NOI2002 荒岛野人
这题其实黑书上有,只是我脑残的没想起来…… 其实就是拓展欧几里得算法 我参看的题解:http://www.cnblogs.com/Rinyo/archive/2012/11/25/2788373.ht ...
- sharepoint 2010 页面添加footer方法 custom footer for sharepoint 2010 master page
转:http://blog.csdn.net/chenxinxian/article/details/8720893 在sharepoint 2010的页面中,我们发现,没有页尾,如果我们需要给页面添 ...
- nopcommerce商城系统--源代码结构和架构
这个文档是让开发者了解nopcommerce解决方案结构的指南.这是新的nopcommerce开发者学习nopcommerce代码的相关文档.首先,nopCommerce源代码是很容易得到的.它是一个 ...
- Mongo DB 安装-及分布式集群部署(初稿)
一.安装步骤, 1, 下载最新的Mongo DB数据库:http://www.mongodb.org/downloads?_ga=1.44426535.2020731121.1421844747\ 下 ...
- myeclipse10 安装 testng插件
下载插件: http://pan.baidu.com/s/1c0pghFE 放到dropins目录
- SVN server 安装 并创建用户访问
1. 下载svn 官网https://www.visualsvn.com/server/download/ 或者其他网站都行,下载是VisualSVN-Server-3.5.6.msi. 2.安装 ...
- Base-Android快速开发框架(二)--数据存储之SharedPreferences
对于App开发者,抽象来说,其实就是将数据以各种各样的方式展示在用户面前以及采集用户的数据.采集用户的数据包括用户的输入.触摸.传感器等,展示的数据通过网络来源于各业务系统,以及用户的 输入数据.在这 ...
- [原][Android]All WebView methods must be called on the same thread.
问题 webView调用JS出错. class TestJS { ...... public TestJS(){ } ...
- python join和split和strip用法
python join 和 split方法的使用,join用来连接字符串,split恰好相反,拆分字符串的. strip()为去除开头结尾指定的字符,空着时是去除空白字符\t,\n,\r意思 1.jo ...