Aizu2970 Permutation Sort
题目大意
给你两个 \(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的更多相关文章
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- HDU 2689 Sort it (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 Sort it Problem Description You want to processe ...
- Bubble Sort (5775)
Bubble Sort Problem Description P is a permutation of the integers from 1 to N(index starting from ...
- Sort with Swap(0, i)
原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/678 题目如下: Given any permutation of the number ...
- CF724B. Batch Sort[枚举]
B. Batch Sort time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode 【31. Next Permutation】
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- permutation II (boss出来了)
题目链接:https://leetcode.com/submissions/detail/55876321/ 自己的做法,30个测试用例通过了29例,终究还是有一个系列类型的是无法通过的,因为自己妄想 ...
- 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 ...
随机推荐
- 在Linux下的安装mysql-5.7.28 心得总结
mysql-5.7.28 在Linux下的安装教程图解 这篇文章主要介绍了mysql-5.7.28 的Linux安装,本文通过图文并茂的形式给大家介绍的非常详细,具有一定的参考借鉴价值,希望给有需要的 ...
- appium -- Xpath定位元素
如文章<Appium基于安卓的各种FindElement的控件定位方法实践>所述,Appium拥有众多获取控件的方法.其中一种就是根据控件所在页面的XPATH来定位控件. 本文就是尝试通过 ...
- 缩点Tarjan算法解析+[题解]受欢迎的牛
(注:我在网上找了一些图,希望原博主不要在意,谢谢,(。☉౪ ⊙。)) 首先来了解什么是强连通分量 有向图强连通分量:在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向 ...
- property内置装饰器函数和@name.setter、@name.deleter
# property # 内置装饰器函数 只在面向对象中使用 # 装饰后效果:将类的方法伪装成属性 # 被property装饰后的方法,不能带除了self外的任何参数 from math import ...
- IDEA 2019.3.3 + Pycharm 2020.2.1 安装包及破解步骤
IDEA IDEA的破解流程就不用再说了,免费试用,添加VMOptions参数,选择破解jar的路径,重启IDEA. 下载地址:链接:https://pan.baidu.com/s/1aTRATVTL ...
- Android10_原理机制系列_Android消息机制(Handler)详述
概述 在Android中的多进程.多线程中提过,只有主线程(UI线程)可以更新UI,其他线程不可以,所以一般耗时操作放到子线程.子线程可以通过Handler将相关信息通知到主线程. Android的消 ...
- yum 方式安装mysql (完整记录)
2016-04-07 学习笔记,源代码安装比较麻烦,还是要尝试一下yum安装和rpm方式安装 一.检查系统是否安装老版本,有的话干掉 #yum list installed | grep mysqlm ...
- python-基础入门-1
Python的打印为 print,等价于c语言的printf 1 print "hello again" 就能打印出hello again,简简单单,就这么一句. 我用的vsc ...
- Vue Springboot (包括后端解决跨域)实现登录验证码功能详细完整版
利用Hutool 基于Vue.ElementUI.Springboot (跨域)实现登录验证码功能 前言 一.Hutool是什么? 二.下面开始步入正题:使用步骤 1.先引入Hutool依赖 2.控制 ...
- 与运算(&)、或运算(|)、异或运算(^)、右移运算符(>>>)本质介绍
按位与运算符(&) 参加运算的两个数据,按二进制位进行"与"运算. 运算规则:0&0=0; 0&1=0; 1&0=0; 1&1= ...