Codeforces 584E Anton and Ira
我们把点分为三类, 向左走的, 向右走的, 不动的。
最完美的情况就是每个点没有走反方向。
每次我们挑选最右边的向右走的去把向左走的交换过来,这样能保证最优。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long
using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, cost, a[N], b[N], to[N];
vector<PII> ans; int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
for(int i = ; i <= n; i++) scanf("%d", &b[i]), to[b[i]] = i;
for(int i = n; i >= ; i--) {
if(to[a[i]] > i) {
int pre = i;
for(int j = i + ; j <= n && to[a[pre]] != pre; j++) {
if(to[a[j]] == j) continue;
ans.push_back(mk(pre, j));
cost += abs(j - pre);
swap(a[pre], a[j]);
pre = j;
}
}
}
printf("%d\n", cost);
printf("%d\n", SZ(ans));
for(auto& t : ans) printf("%d %d\n", t.fi, t.se);
return ;
} /*
*/
Codeforces 584E Anton and Ira的更多相关文章
- Codeforces 584E - Anton and Ira - [贪心]
题目链接:https://codeforces.com/contest/584/problem/E 题意: 给两个 $1 \sim n$ 的排列 $p,s$,交换 $p_i,p_j$ 两个元素需要花费 ...
- codeforces 584E Anton and Ira [想法题]
题意简述: 给定一个$1$到$n(n<=2000)$的初始排列以及最终排列 我们每次可以选取位置为$i$和$j$的 并交换它们的位置 花费为$ |i-j| $ 求从初始状态变换到末状态所需最小花 ...
- 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 ...
- 【25.00%】【codeforces 584E】Anton and Ira
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
- Codeforces 593B Anton and Lines
LINK time limit per test 1 second memory limit per test 256 megabytes input standard input output st ...
- Codeforces 734F Anton and School(位运算)
[题目链接] http://codeforces.com/problemset/problem/734/F [题目大意] 给出数列b和数列c,求数列a,如果不存在则输出-1 [题解] 我们发现: bi ...
- [刷题]Codeforces 785D - Anton and School - 2
Description As you probably know, Anton goes to school. One of the school subjects that Anton studie ...
- Codeforces 785D - Anton and School - 2 - [范德蒙德恒等式][快速幂+逆元]
题目链接:https://codeforces.com/problemset/problem/785/D 题解: 首先很好想的,如果我们预处理出每个 "(" 的左边还有 $x$ 个 ...
随机推荐
- POJ 1062 昂贵的聘礼(图论,最短路径)
POJ 1062 昂贵的聘礼(图论,最短路径) Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女 ...
- POJ 2235 Frogger / UVA 534 Frogger /ZOJ 1942 Frogger(图论,最短路径)
POJ 2235 Frogger / UVA 534 Frogger /ZOJ 1942 Frogger(图论,最短路径) Description Freddy Frog is sitting on ...
- (转)Maven学习总结(六)——Maven与Eclipse整合
孤傲苍狼只为成功找方法,不为失败找借口! Maven学习总结(六)——Maven与Eclipse整合 一.安装Maven插件 下载下来的maven插件如下图所示:,插件存放的路径是:E:/MavenP ...
- webpack实现“热更新”和“热加载”(webpack3.6新增)
之前的博文有提到怎么样去实现热更新,但是居然要用到另外一个node框架(express)而且新增了一个入口(entry)两个插件(plugin)来实现,大大提高了配置的复杂度,这样其实对使用webpa ...
- Python微信红包算法
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...
- MySQL日志功能详解
MySQL日志功能详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查询日志 它是用来保存所有跟查询相关的日志,这种日志类型默认是关闭状态的,因为MySQL的用户有很多,如果 ...
- js实现表单提交submit(),onsubmit
通常表单的提交有两种方式,一是直接通过html的form提交,代码如下: <form action="" method="" id="forms ...
- 20155203 2016-2017-4 《Java程序设计》第6周学习总结
20155203 2016-2017-4 <Java程序设计>第6周学习总结 教材学习内容总结 重载和覆盖:重载是将函数的名称和方法不变(名称可以有一些变动),只改变参数列表,相当于和原来 ...
- CSS font系列
font-family font-family: Verdana,Helvetica,Arial,"Microsoft YaHei",sans-serif; font-family ...
- CSS float浅析
写在开篇: 浮动属性的设计初衷,只是为了实现文本环绕效果! 时刻牢记这一点,才能正确使用浮动. 由于浮动元素脱离文档流,它后面的块级元素会忽略它的存在,占据它原本的位置,但是这个块级元素中的内联元素, ...