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的更多相关文章

  1. Codeforces 584E - Anton and Ira - [贪心]

    题目链接:https://codeforces.com/contest/584/problem/E 题意: 给两个 $1 \sim n$ 的排列 $p,s$,交换 $p_i,p_j$ 两个元素需要花费 ...

  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. 【25.00%】【codeforces 584E】Anton and Ira

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

  5. Codeforces 734E. Anton and Tree 搜索

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

  6. Codeforces 593B Anton and Lines

    LINK time limit per test 1 second memory limit per test 256 megabytes input standard input output st ...

  7. Codeforces 734F Anton and School(位运算)

    [题目链接] http://codeforces.com/problemset/problem/734/F [题目大意] 给出数列b和数列c,求数列a,如果不存在则输出-1 [题解] 我们发现: bi ...

  8. [刷题]Codeforces 785D - Anton and School - 2

    Description As you probably know, Anton goes to school. One of the school subjects that Anton studie ...

  9. Codeforces 785D - Anton and School - 2 - [范德蒙德恒等式][快速幂+逆元]

    题目链接:https://codeforces.com/problemset/problem/785/D 题解: 首先很好想的,如果我们预处理出每个 "(" 的左边还有 $x$ 个 ...

随机推荐

  1. MT【104】高斯函数找周期

    分析:$t(n)=n-[\frac{n}{2}]-[\frac{n}{3}]-[\frac{n}{6}]$的周期为6,故 $\sum\limits_{n=1}^{2014}(n-t(n))=\sum\ ...

  2. 【题解】 [ZJOI2008] 泡泡堂(贪心/二分图/动态规划)

    懒得复制,戳我戳我 Solution: 就是有一个贪心策略:(以下假设使\(A\)队分数更高) \(First:\)比较两个分值的最小值,如果\(A\)最小分比\(B\)最小分大就直接比较两个最小的, ...

  3. 【转】 CRC循环冗余校验码

    1.CRC CRC循环冗余校验码是数据通信中的一种查错校验码. 循环冗余检查对数据进行多项式计算,将计算结果附加在帧后面,接收数据的设备执行模2运算,保证数据传输的正确性和完整性. 2.模2除法 ①不 ...

  4. spring data jpa createNativeQuery 错误 Unknown entity

    springdatajpa本地查询的时候,报错:org.hibernate.MappingException: Unknown entity: com.hzxc.guesssong.model.Que ...

  5. 几种简单的排序算法(JAVA)

    几种排序算法(JAVA) 一.代码 package com.hdwang; import java.util.Arrays; /** * Created by admin on 2017/1/20. ...

  6. 解题:POI2008 Building blocks

    题面 显然我们需要考虑每一个区间,而这个问题显然我们都会做,这不就是这道题么,也就是说假如中位数是$mid$,区间和是$sum$,那么代价就是$\sum\limits_{i=l}^r |mid-num ...

  7. AC自动机——多个kmp匹配

    (并不能自动AC) 介绍: Aho-Corasick automaton,最经典的处理多个模式串的匹配问题. 是kmp和字典树的结合. 精髓与灵魂: ①利用trie处理多个模式串 ②引入fail指针. ...

  8. 修改Visual Studio项目中程序集信息默认公司名称的两种方法

    这个公司名就是安装系统时注册的单位名称.可以通过修改注册表修改 Windows 系统的注册信息,方法如下:1.在开始"运行"中输入regedit,打开注册表编辑器.2.依次展开:H ...

  9. INF文件详解

    安装信息(Setup Information)文件是Windows系统支持的一种安装信息存放文件,一般以INF作为扩展名,因此也叫INF文件.安装信息INF文件与Windows内建的安装服务引擎(AP ...

  10. 鸟哥的Linux私房菜——第九章

    视频链接,推荐看B站 土豆网:http://www.tudou.com/programs/view/XmMDbjJHJC8 B站:http://www.bilibili.com/video/av966 ...