51nod 1574 排列转换(贪心)
来源:互联网 发布:手机放线软件 编辑:程序博客网 时间:2024/06/04 19:21
1574 排列转换
题目来源: CodeForces
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
收藏
关注
现在有两个长度为n的排列p和s。要求通过交换使得p变成s。交换
Input
单组测试数据。第一行有一个整数n (1≤n≤200000),表示排列的长度。第二行有n个范围是1到n的整数,表示排列p。每个整数只出现一次。第三行有n个范围是1到n的整数,表示排列s。每个整数只出现一次。
Output
输出一个整数,表示从排列p变到s最少要多少代价。
Input示例
样例输入144 2 1 33 2 4 1
Output示例
样例输出13
System Message (题目提供者)
Visual C++的运行时限为:1000 ms ,空间限制为:131072 KB 示例及语言说明请按这里
允许其他 AC 的用户查看此代码,分享代码才能查看别人的代码并有机会获得勋章
一个数每次往左右相邻的位置移动花费的代价为1,为了尽可能的优,正确位置不再动,并且当一个数移动时,
另一个数一定会跟着移动,呢答案就是所有数的 | 次位置-初位置 |/2
#include<set> #include<map> #include<stack> #include<queue> #include<vector> #include<string> #include<math.h> #include<time.h> #include<stdio.h> #include<iostream> #include<string.h> #include<stdlib.h> #include<algorithm> #include<functional> using namespace std;typedef long long ll;#define inf 1000000000 #define mod 1000000007 #define maxn 200005 #define PI 3.1415926 #define lowbit(x) (x&-x) #define eps 1e-9ll a[maxn];int main(void){ll n, i, ans = 0, x;scanf("%lld", &n);for (i = 1;i <= n;i++){scanf("%lld", &x);a[x] = i;}for (i = 1;i <= n;i++){scanf("%lld", &x);ans += abs(i - a[x]);}printf("%lld\n", ans / 2);return 0;}
阅读全文
1 0
- 51nod 1574 排列转换(贪心)
- 51 nod 1574 排列转换(思维 贪心)
- 51nod 1574 排列转换(贪心+鸽巢原理)
- ★★51nod 1574 排列转换 (贪心思维题)
- 51Nod-1574-排列转换
- 51nod 1574 排列转换
- 51nod 1574: 排列转换
- 51nod-1574-排列转换
- 51nod 1574 排列转换(抽屉原理)
- 51 nod 砝码称重(贪心+进制转换思想)
- 51nod 1432 (贪心)
- 【51Nod】1384 - 全排列(STL)
- 51nod 1384 全排列(next_permutation)
- 51nod 1020 逆序排列(dp)
- 51Nod-1384 全排列(回溯)
- 51nod 全排列
- 51Nod 1384:全排列(递归求全排列)
- 51Nod 算法马拉松17 最好的排列 贪心求解加高精度
- 异常处理
- 充满仪式感的生活
- 存储器层次结构示例
- keepalived+nginx主从配置
- JSP(一)
- 51nod 1574 排列转换(贪心)
- Md5加密
- HDU 3709 Balanced Number
- 基于FreeRTOS与MQTT的物联网技术应用系列——步进电机控制(一)前言
- 超越seo竞争对手的策略
- 今天开始,以后复习的方式改为写博客总结
- SpringBoot JPA接口使用方法
- Github + Git + Maven + tomcat + Jenkins的可持续集成部署
- Spring Cloud入门之Spring Cloud Config(高可用,刷新配置)