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$ 个 ...
随机推荐
- 自学Zabbix3.10.1.5-事件通知Notifications upon events-媒介类型Script
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix3.10.1.5-事件通知Notifications upon events-媒介 ...
- c#(.Net)解析xml
1.一般处理 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml("<?xml version=\"1.0\&quo ...
- SQL Server 一些查询技巧
--1.[行列转换] --列转行 USE tempdb GO IF (OBJECT_ID('DEPT') IS NOT NULL) DROP TABLE DEPT CREATE TABLE DEPT( ...
- Hadoop生态圈-Azkaban部署实战
Hadoop生态圈-Azkaban部署实战 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Azkaban部署流程 1>.上传azkaban程序并创建解压目录 [yinz ...
- 文件通过svn updata更新不到,并且svn st显示被删除的解决办法
不知道什么原因导致某些文件丢失,svn updata更新后仍然没有找到,采用svn st 显示这些文件被删除,svn reslove 也解决不了,头疼了很久,最近突然解决了,具体步骤如下(已经过验证) ...
- angular.module()参数问题
var app = angular.module('myApp', []); 第二个参数是依赖的模块,因为这里不需要依赖其它模块,因此为空,但是[]不能省略.
- SQL记录-PLSQL数组
PL/SQL数组 PL/SQL程序设计语言提供叫一种叫做VARRAY的数据结构,其可存储相同类型元素的一个固定大小的连续集合.VARRAY用于存储数据的有序集合,但它往往是更加有用认为数组作为相同类型 ...
- windows命令快捷启动应用-----window小技巧
前言 装逼的道路总是这么漫长 而又充满激情.对于崇尚技术的男儿,了解计算机的世界,是我一辈子都是在追寻的.看着各种黑客电影,有那个大牛还需要鼠标的辅助,想想都是那么的令人兴奋 为了有那么一天的到来,我 ...
- 机器学习算法整理(七)支持向量机以及SMO算法实现
以下均为自己看视频做的笔记,自用,侵删! 还参考了:http://www.ai-start.com/ml2014/ 在监督学习中,许多学习算法的性能都非常类似,因此,重要的不是你该选择使用学习算法A还 ...
- J2EE完全手册(二)
1.2 客户端 (Web客户端,应用程序客户端) 1.2.1 Web客户端: 一般简单讲,就是显示由在Web层的web组件中生成的包含Html 及 XML标记语言的动态Web页面(.jsp[.do]) ...