题目大意

给你两个 \(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. dst_output发包

    不管是收到报文转发还是本机发送报文,最后都会调用dst_output /* Output packet to network from transport. */ static inline int ...

  2. lseek系统调用

    文件的随机读写.目前为止,文件都是顺序访问.读写都是从当前文件的偏移位置开始,然后文件偏移值自动的增加到刚好超出读或者写结束的位置是它为下一次作好准备.在linux中有文件偏移.使得随机访问变得简单, ...

  3. Linux mysql 修改密码 三种方式(转载)

    注明:本文为转载,原文地址:https://www.cnblogs.com/chuckjam/archive/2018/08/10/9456255.html 前言 有时我们会忘记Mysql的密码,或者 ...

  4. CephFS用户认证格式写错的问题

    问题三: CephFS(James Gallagher) 问题原文 Hi, I'm looking to implement the CephFS on my Firefly release (v0. ...

  5. pytorch框架对RTX 2080Ti RTX 3090的支持与性能测试

    时间点:202011-18 一.背景 2020年9月nvidia发布了30系列的显卡.比起20系列网上的评价是:性能翻倍,价格减半. 最近正好本人手上有RTX 2080Ti 和 RTX 3090,所以 ...

  6. [LeetCode题解]876. 链表的中间结点 | 快慢指针

    解题思路 使用快慢指针.这里要注意的是,while 的条件会影响当中间节点有两个时,slow 指向的是第一个,还是第二个节点. // 返回的是第一个 while(fast.next != null & ...

  7. IGH_Master主站配置驱动伺服电机和变频器总结

    IGH_Master主站配置驱动伺服电机和变频器总结 Ethercat是倍福公司提出的一种工业现场总线协议,具有很好的实时性,IGH是一种开源的Ethercat主站实现协议,本文总结了一下使用IGH_ ...

  8. 在IDM上设置防止过度抓取网站信息

    在使用Internet Download Manager(IDM)下载器时,有时会发现IDM自带的抓取功能过于强大,以至于有时会抓取一些无效的链接.那么,该如何避免IDM的过度抓取呢? 图1:IDM的 ...

  9. HTML常用标签总结 [建议收藏]

    好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star 1. 标题标签 <h1> </h1> ...

  10. Java之 函数(五)

    第一部分 : IDEA开发工具 1.数组 1.1 数组介绍 ​ 数组就是存储数据长度固定的容器,存储多个数据的数据类型要一致. 1.2 数组的定义格式 1.2.1 第一种格式 ​ 数据类型[] 数组名 ...