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 ...
随机推荐
- 幻读在 InnoDB 中是被如何解决的?(转)
在MySQL事务初识中,我们了解到不同的事务隔离级别会引发不同的问题,如在 RR 级别下会出现幻读.但如果将存储引擎选为 InnoDB ,在 RR 级别下,幻读的问题就会被解决.在这篇文章中,会先介绍 ...
- mybatis-plus使用p6spy 插件进行sql性能分析
1.依赖 <dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId& ...
- java开发两年,这些线程知识你都不知道,你怎么涨薪?
前言 什么是线程:程序中负责执行的哪个东东就叫做线程(执行路线,进程内部的执行序列),或者说是进程的子任务. Java中实现多线程有几种方法 继承Thread类: 实现Runnable接口: 实现Ca ...
- 【建议收藏】一份阿里大牛花了三天整理出来的XML学习笔记,写的非常详细
1. 什么是XML? XML 指可扩展标记语言(EXtensible Markup Language)XML 是一种标记语言,很类似 HTMLXML 的设计宗旨是传输数据,而非显示数据XML 标签没有 ...
- 如何用CDR做出毛笔字效果
不仅仅是水墨字,毛笔字在CDR中的制作也是很简单的.一般来讲,水墨字其实跟毛笔字有相通之处,也可以说毛笔字是水墨字的一种,在CDR中的实现也是既简单又实用的. 方法一:艺术笔工具 艺术笔工具是比较便捷 ...
- CorelDRAW常用工具之橡皮擦工具
很多作图以及设计软件都会自带橡皮擦工具,但对于专业做平面设计的小伙伴来说,普通的橡皮工具肯定是无法满足日常作图需求的,今天来看看CorelDRAW的橡皮擦能玩出什么花样来. 1.擦除对象 CorelD ...
- 【PUPPETEER】初探之元素获取(二)
一.涉及的知识点 如何使用css selector 常用元素获取 $ 元素选择 type (api 输入) click (api 点击) 二.学习网址 https://github.com/Googl ...
- ?.可选链操作符( ?. ) 可选链运算符可防止抛出 TypeError: Cannot read property ’xxx' of undefined。
可选链操作符( ?. )允许读取位于连接对象链深处的属性的值,而不必明确验证链中的每个引用是否有效.?. 操作符的功能类似于 . 链式操作符,不同之处在于,在引用为空(nullish ) (null ...
- C语言讲义——注释
注释 什么是注释? --注释写在代码中的文字,不参与代码编译,不影响运行结果. 为什么要注释?--让代码可读性更强. C语言有两种注释: 单行注释 // 多行注释 /* */ 多行注释可以只有一行, ...
- (转)React学习笔记(干货满满)
1. React 长什么样 React 是 facebook 开源出来的一套前端方案,官网在 https://reactjs.org . 先看一个简单的样子: <!DOCTYPE html> ...