题目链接:https://codeforces.com/contest/584/problem/E

题意:

给两个 $1 \sim n$ 的排列 $p,s$,交换 $p_i,p_j$ 两个元素需要花费 $|i-j|$,要求你用最少的钱交换 $p$ 中的元素使得其变成 $s$。

题解:

思路很简单,给个例子如下:

$p$:$5,1,2,3,4,6$

$s$:$3,4,1,6,2,5$

我们不难发现,像:

$p$:$5,1,\underline{2},\underline{3},4,6$

$s$:$\underline{3},4,1,6,\underline{2},5$

或者

$p$:$5,1,\underline{2},3,\underline{4},6$

$s$:$3,\underline{4},1,6,\underline{2},5$

这样的情况,交换两个数字必定是不会亏的,而且是必须要花费的,所以每次都直接暴力枚举找符合的 $(i,j)$,一旦遇到就立刻把它俩交换即可。

当然不能太过暴力地 $O(n^2)$ 去找 $(i,j)$,需要一点剪枝和区间控制。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
#define mk(x,y) make_pair(x,y)
#define fi first
#define se second
const int maxn=2e3+;
int n;
int s[maxn],t[maxn];
int pos[maxn]; P Find()
{
int x,y;
for(int i=;i<=n;i++)
{
x=pos[s[i]];
if(i<x)
{
for(int j=i+;j<=x;j++)
{
y=pos[s[j]];
if(y<=i) return mk(i,j);
}
}
}
} int main()
{
ios::sync_with_stdio();
cin.tie(), cout.tie(); cin>>n;
for(int i=;i<=n;i++) cin>>s[i];
for(int i=;i<=n;i++) cin>>t[i], pos[t[i]]=i; int diff=;
for(int i=;i<=n;i++) diff+=(s[i]!=t[i]); vector<P> op;
int cost=;
while(diff)
{
P r=Find();
swap(s[r.fi],s[r.se]);
//printf("%d <-> %d\n",r.fi,r.se);
op.push_back(r);
cost+=abs(r.fi-r.se);
diff-=(s[r.fi]==t[r.fi]);
diff-=(s[r.se]==t[r.se]);
} cout<<cost<<'\n';
cout<<op.size()<<'\n';
for(auto x:op) cout<<x.fi<<" "<<x.se<<'\n';
}

Codeforces 584E - Anton and Ira - [贪心]的更多相关文章

  1. Codeforces 584E Anton and Ira

    Anton and Ira 我们把点分为三类, 向左走的, 向右走的, 不动的. 最完美的情况就是每个点没有走反方向. 每次我们挑选最右边的向右走的去把向左走的交换过来,这样能保证最优. #inclu ...

  2. codeforces 584E Anton and Ira [想法题]

    题意简述: 给定一个$1$到$n(n<=2000)$的初始排列以及最终排列 我们每次可以选取位置为$i$和$j$的 并交换它们的位置 花费为$ |i-j| $ 求从初始状态变换到末状态所需最小花 ...

  3. 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 ...

  4. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  5. [Codeforces 1214A]Optimal Currency Exchange(贪心)

    [Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...

  6. 【25.00%】【codeforces 584E】Anton and Ira

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 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 ...

  8. Codeforces 734E. Anton and Tree 搜索

    E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...

  9. codeforces 349B Color the Fence 贪心,思维

    1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...

随机推荐

  1. vim配置go语法高亮

    操作系统 : CentOS7.3.1611_x64 go 版本 : go1.8.3 linux/amd64 vim版本 :version 7.4.160 vim配置go语言语法高亮的问题已经遇到过好几 ...

  2. 【PMP】关键路径法与关键链法

    通俗理解 关键路径法:把项目上的资源都事先全部分到每个活动上. 关键链法:每个活动不打富余,项目经理自己掌握资源,哪个成员执行过程中遇到困难,再给他单独分配资源. PMBOK定义 关键路径法:关键路径 ...

  3. [dubbo] Dubbo API 笔记——配置参考

    schema 配置参考 所有配置项分为三大类 服务发现:表示该配置项用于服务的注册与发现,目的是让消费方找到提供方 服务治理:表示该配置项用于治理服务间的关系,或为开发测试提供便利条件 性能调优:表示 ...

  4. Device does not seem to be present [常见错误解决]

    一.故障现象: [root@c1node01 ~]# service network restart Shutting down loopback insterface:                ...

  5. delphi 导出到excel的7种方法

    本文来自 爱好者8888 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/kpc2000/article/details/17066823?utm_source=cop ...

  6. 20款最好的JavaScript开发框架

    JavaScript语言有多种方式,创建交互式网站,Web应用程序.基本的JavaScript框架是预先写好的JavaScript代码集.这些JavaScript框架也被称为JavaScript库,开 ...

  7. Linux常用指令笔记

    目标:统计当前目录下java文件的个数 指令:`ls -R ./ | grep .java$ | wc -l` 原理:`ls -R ./`列出当前文件夹下的所有FILE,包括目录以及文件;`grep ...

  8. 用反卷积(Deconvnet)可视化理解卷积神经网络还有使用tensorboard

    『cs231n』卷积神经网络的可视化与进一步理解 深度学习小白——卷积神经网络可视化(二) TensorBoard--TensorFlow可视化 原文地址:http://blog.csdn.net/h ...

  9. OpenGL 获取当前屏幕坐标的三维坐标(gluUnProject使用例子 Qt)

    之前使用VS+glut实现了gluUnProject使用例子,用于渲染管道的逆过程,将屏幕坐标转换为opengl三维坐标,本文将尝试使用QT来实现. 代码如下:  main.cpp  12345678 ...

  10. 第一篇:你不一定了解的"推荐系统"

    前言 [推荐系统 - 基础教程]可能是穆晨的所有博文里,最有趣最好玩的一个系列了^ ^. 作为该系列的[入门篇],本文将轻松愉快地向读者介绍推荐系统这项大数据领域中的热门技术. 为什么要有推荐系统? ...