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$ 个 ...
随机推荐
- bzoj4152 The Captain (dijkstra)
做dijkstra,但只需要贪心地把每个点连到它左边.右边.上边.下面的第一个点就可以了 #include<bits/stdc++.h> #define pa pair<int,in ...
- Burpsuit分块传输插件绕WAF原理和技巧(转)
0x00 原理 给服务器发送payload数据包,使得waf无法识别出payload,当apache,tomcat等web容器能正常解析其内容.如图一所示 0x02 实验环境 本机win10+x ...
- 【转】在windows中使用Intellij Idea时选择自定义的64位JVM
原文地址:https://www.iflym.com/index.php/code/201404190001.html 本文英文原文自:https://intellij-support.jetbrai ...
- C# 分析 IIS 日志(Log)
由于最近又要对 IIS日志 (Log) 分析,以便得出各个搜索引擎每日抓取的频率,所以这两天一直在尝试各个办法来分析 IIS 日志 (Log),其中尝试过:导入数据库.Log parser.Powse ...
- 《剑指offer》— JavaScript(30)连续子数组的最大和
连续子数组的最大和 题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好 ...
- Css设置img属性让图片水平居中/居左/居右的写法
图片的居中显示css有很多方法,但在很多情况下有的方法无效,无意发现这个系统的官方处理图片居中,居左,居右的css写法,喜欢的朋友可以收藏下哦 图片的居中显示css有很多方法,但在很多情况下有的方法无 ...
- CGI浏览器与服务器的交互
一直在做项目,跟着写前端后端,却没有思考一个问题:前端和后端为什么能够进行通信?为什么能够将HTML页面的内容传输给后台,然后又将结果反馈给前端? 寒假偶尔看到了这个问题,也解决了我的疑惑,这是基于C ...
- Debian8 系统修改语言设置成英文
本文摘自 https://wiki.debian.org/ChangeLanguage ,感谢作者 * First, you have to set environment variables suc ...
- SQL记录-PLSQL集合
PL/SQL集合 集合是一个有序组具有相同的数据类型的元素.每个元素进行标识的唯一标表示其在集合中的位置. PL/SQL提供了三种集合类型: 索引表或关联数组 嵌套表 可变大小的数组或变长数组 Ora ...
- MongoDB探索之路(二)——系统设计之CRUD
1.构造实体类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...