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 ...
随机推荐
- CTDB使用radosobject作为lockfile
前言 服务器的服务做HA有很多种方式,其中有一种就是是用CTDB,之前这个是独立的软件来做HA的,现在已经跟着SAMBA主线里面了,也就是跟着samba发行包一起发行 之前CTDB的模式是需要有一个共 ...
- Python_selenium案例:
selenium案例 #coding=utf-8 #select下拉框处理 from selenium import webdriver from selenium.webdriver.common. ...
- appium 数据参数化 登录模块
下面是我最近学习的PYTHON的登录代码: class test(object): def getdic(self): d = {'username': '13', 'password': '1111 ...
- [原题复现+审计][0CTF 2016] WEB piapiapia(反序列化、数组绕过)[改变序列化长度,导致反序列化漏洞]
简介 原题复现: 考察知识点:反序列化.数组绕过 线上平台:https://buuoj.cn(北京联合大学公开的CTF平台) 榆林学院内可使用信安协会内部的CTF训练平台找到此题 漏洞学习 数组 ...
- 阿里四面P7稳了,得亏我会这些Spring面试题,果然大厂都爱问它们
前言 先说一下本人情况吧,末流985毕业,毕业之后一直在一家不大不小的公司里安稳上班.上半年因为疫情的原因公司调整了工资,我也是随波逐流跟随大家辞了职.辞职之后向阿里.字节这些都投了简历(但是只收到了 ...
- 一键加Q群的实现
打开网址 选择创建的群 选择所需要的二维码或者代码
- iOS中如何使定时器NSTimer不受UIScrollView滑动所影响
以下是使用 scheduledTimerWithTimeInterval 方法来实现定时器 - (void)addTimer { NSTimer scheduledTimerWithTimeInter ...
- uniapp自定义picker城市多级联动组件
uniapp自定义picker城市多级联动组件 支持多端--h5.app.微信小程序.支付宝小程序... 支持自定义配置picker插件级数 支持无限级 注意事项:插件传入数据格式为children树 ...
- 【CF600E】Lomsat gelral——树上启发式合并
(题面来自luogu) 题意翻译 一棵树有n个结点,每个结点都是一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号的和. ci <= n <= 1e5 裸题.统计时先扫一遍得到出 ...
- JJWT 使用示例
一.添加依赖包 <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-api ...