题目链接: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. 第二天学习笔记:(MDN HTML学习、web安全策略与常见攻击、语义化)

    一:Web入门 1:web文件命名 在文件名中应使用连字符(-).搜索引擎把连字符当作一个词的分隔符, 但不会以这种方式处理下划线. 养成在文件夹和文件名中使用小写,并且使用短横线而不是空格来分隔的习 ...

  2. 转发:CentOS下tar压缩排除某个文件夹或文件及解压

     一.压缩 一般直接用tar命令打包很简单,直接使用 tar -zcvf test.tar.gz test 即可. 在很多时候,我们要对某一个目录打包,而这个目录下有几十个子目录和子文件,我们需要在打 ...

  3. 关于Discuz! X系列远程代码执行漏洞

    一.漏洞起源 突然有同事反馈,无法注册 看到这里不了解的同行估计一年懵逼,这里也是常用的漏洞攻击,可以肯定的是  badwords.php文件被修改了 ,可以查看这个文件内容 <?php $_C ...

  4. R语言中的回归诊断-- car包

    如何判断我们的线性回归模型是正确的? 1.回归诊断的基本方法opar<-par(no.readOnly=TRUE) fit <- lm(weight ~ height, data = wo ...

  5. linux 目录/sys 解析

    今天搞树莓派,遇到/sys这个目录,不太清楚,先对/sys目录知识进行一个整理 首先,对 /sys目录下的各个子目录进行具体说明: /sys下的子目录 内容 /sys/devices 该目录下是全局设 ...

  6. class<T>和 class<?>类型

    ? 表示不确定的java类型. T 表示java类型 Class<T>在实例化的时候,T要替换成具体类Class<?>它是个通配泛型,?可以代表任何类型<? extend ...

  7. js 性能优化利器:prepack

    1. js 性能优化 js 本身是没有像 python 一样的预编译功能,更没有像 java 一样的编译功能,所以,这里所说的 js 代码预编译 只是通过工具实现的类似功能而已. 这就要提到 prep ...

  8. idea系列ide给git增加push按钮

    第一步 打开设置 Appearance & Behavior -- Menus and ToolBars,选中VscNavBarToolBarActios,然后点击"+"添 ...

  9. C语言 · 单词数统计

    单词数统计 输入一个字符串,求它包含多少个单词. 单词间以一个或者多个空格分开. 第一个单词前,最后一个单词后也可能有0到多个空格. 比如:" abc    xyz" 包含两个单词 ...

  10. OpenCV + python 实现人脸检测(基于照片和视频进行检测)

    OpenCV + python 实现人脸检测(基于照片和视频进行检测) Haar-like 通俗的来讲,就是作为人脸特征即可. Haar特征值反映了图像的灰度变化情况.例如:脸部的一些特征能由矩形特征 ...