题目大意

给你两个 \(n\) 个整数的排列,第一个排列表示原排列,第二个排列表示第 \(i\) 个数可以和i变成第 \(g_i\) 个数,问,最少对所有数进行几次操作可以使原排列变为有序的排列。

题解

首先,我们可以利用第二个排列建图,易得每一个点只有一个出度,一个入度,所以这幅图只由简单环和自环组成。

我们还可以发现,在环上跑大于环的长度的距离等同于跑两点之间的直线距离,也就是说如果环的长度为 \(cnt_i\) ,两点之间的直线距离为 \(x_i\) ,我们要求的距离为 \(d\) ,那么 $d\equiv x_i(mod~cnt_i) $ 。根据每一个 \(i\) ,我们都可以列出这么一个方程,于是问题就转变为求 \(n\) 个同余方程的最小公共解,使用扩展中国剩余定理即可。

代码如下:

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=205;
int n;
int a[N],to[N];
int e[N][N];
int tag[N],cnt[N];
void dfs(int p,int nam)
{
tag[p]=nam;
cnt[nam]++;
if(!tag[to[p]])
dfs(to[p],nam);
return ;
}
int gcd(int a,int b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
void exgcd(int a,int b,int &x,int &y)
{
if(b==0)
{
x=1,y=0;
return ;
}
exgcd(b,a%b,x,y);
int tmp=x;
x=y;
y=tmp-a/b*y;
}
signed main()
{
cin>>n;
for(int i=1;i<=n;++i)
cin>>a[i];
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
e[i][j]=1e18+5;
for(int i=1;i<=n;++i)
{
cin>>to[i];
e[i][to[i]]=1;
}
for(int i=1;i<=n;++i)
e[i][i]=0;
for(int k=1;k<=n;++k)
{
for(int i=1;i<=n;++i)
{
for(int j=1;j<=n;++j)
e[i][j]=min(e[i][j],e[i][k]+e[k][j]);
}
}
for(int i=1;i<=n;++i)
{
if(e[a[i]][i]>=1e18+5)
{
printf("-1\n");
return 0;
}
}
for(int i=1;i<=n;++i)
{
if(!tag[i])
dfs(i,i);
}
int M=cnt[tag[1]],ans=e[a[1]][1];
for(int i=2;i<=n;++i)
{
int x,y,tmp=gcd(M,cnt[tag[i]]),now=((e[a[i]][i]-ans)%cnt[tag[i]]+cnt[tag[i]])%cnt[tag[i]];
if(now%tmp)
{
printf("-1\n");
return 0;
}
exgcd(M,cnt[tag[i]],x,y);
x*=now/tmp;
ans+=x*M;
M*=cnt[tag[i]]/tmp;
ans=(ans%M+M)%M;
}
printf("%lld\n",ans);
return 0;
}

Aizu2970 Permutation Sort的更多相关文章

  1. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  2. HDU 2689 Sort it (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 Sort it Problem Description You want to processe ...

  3. Bubble Sort (5775)

    Bubble Sort Problem Description   P is a permutation of the integers from 1 to N(index starting from ...

  4. Sort with Swap(0, i)

    原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/678 题目如下: Given any permutation of the number ...

  5. CF724B. Batch Sort[枚举]

    B. Batch Sort time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  6. Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  7. LeetCode 【31. Next Permutation】

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  8. permutation II (boss出来了)

    题目链接:https://leetcode.com/submissions/detail/55876321/ 自己的做法,30个测试用例通过了29例,终究还是有一个系列类型的是无法通过的,因为自己妄想 ...

  9. PAT 1067. Sort with Swap(0,*)

    1067. Sort with Swap(0,*) (25)   Given any permutation of the numbers {0, 1, 2,..., N-1}, it is easy ...

随机推荐

  1. linux中/etc/passwd和/etc/shadow文件说明

    /etc/passwd是用来存储登陆用户信息: [root@localhost test]# cat /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x ...

  2. sync_with_stdio(false)和cin.tie(NULL)

    std::ios::sync_with_stdio(false) 这个函数相当于是否兼容stdio的开关,默认为true C++为了兼容C,保证程序在使用了std::printf和std::cout的 ...

  3. 2020-11-02(三年之约D92)-优秀不是一种行为,而是一种习惯

    1.阅读:<软技能-代码之外的生存指南>- 第45章 培养习惯:刷新你的代码 成就我们的恰恰就是那些不断重复做的事情.因此,优秀不是一种行为,而是一种习惯--亚里士多德 习惯主要由三个要素 ...

  4. python-Requests模块的使用

    1. Requests简介 Requests模块是一个用于网络访问的模块,其实类似的模块有很多,比如urllib,urllib2,httplib,httplib2,他们基本都提供相似的功能,那为什么R ...

  5. kali 系列学习06 - 攻击之密码破解

    参考书 <kali linux渗透测试技术详解> 清华大学出版社  2015 杨波 一.medusa使用 原理:并行登陆破解 1.只输入单个命令,是查看帮助 root@kali:/tool ...

  6. NPOI读写Excel组件封装Excel导入导出组件

    后台管理系统多数情况会与Excel打交道,常见的就是Excel的导入导出,对于Excel的操作往往是繁琐且容易出错的,对于后台系统的导入导出交互过程往往是固定的,对于这部分操作,我们可以抽离出公共组件 ...

  7. 【MathType教学】表示分类的大括号怎么打

    大括号是一种常见的数学符号,可以用于集合.分段函数中,其实大括号还可以用来总结数学知识,比如对三角形进行分类,此时用的大括号可以称为表示分类的大括号.MathType作为专业的数学公式编辑器,可以快速 ...

  8. CorelDRAW常用工具之橡皮擦工具

    很多作图以及设计软件都会自带橡皮擦工具,但对于专业做平面设计的小伙伴来说,普通的橡皮工具肯定是无法满足日常作图需求的,今天来看看CorelDRAW的橡皮擦能玩出什么花样来. 1.擦除对象 CorelD ...

  9. go学习路线资料

    编辑器 JetBrains公司出品的,goland go初步学习路线 Go 指南 如何使用Go编程 实效Go编程 Go by Example 中文版 参考: Go 语言学习资料与社区索引 Go入门指南 ...

  10. J2EE基本概念

    XO POJO:Plain Ordinary Java Object,简单java对象 PO:Persistant Object,持久层对象(对应数据库中一条记录) BO:Business Objec ...