当前位置:网站首页>AcWing 1921. 重新排列奶牛(环图)
AcWing 1921. 重新排列奶牛(环图)
2022-06-12 10:49:00 【柃歌】
【题目描述】
农夫约翰的 N N N头奶牛排成一排,编号 1 ∼ N 1\sim N 1∼N。
它们的排序可以由一个数组 A A A来描述,其中 A ( i ) A(i) A(i)是位于位置 i i i的奶牛的编号。
约翰希望将它们重新排列为一个不同的顺序。
新顺序用数组 B B B来描述,其中 B ( i ) B(i) B(i)是位于位置 i i i的奶牛的编号。
假设奶牛开始时的顺序为:
A = 5 1 4 2 3
并假设约翰希望它们重新排列为:
B = 2 5 3 1 4
为了从 A A A顺序重新排列为 B B B顺序,奶牛们进行了许多“循环”移位。
所谓循环移位,是指挑选排列中的若干头奶牛分在一组,组中奶牛进行循环移动位置,即第一头奶牛移动至第二头奶牛的位置,第二头奶牛移动至第三头奶牛的位置,以此类推,最后一头奶牛移动至第一头奶牛的位置。
如上例中,将 5 , 1 , 2 5,1,2 5,1,2号奶牛分在一组进行循环移位,移动过后, 5 5 5号奶牛移动至位置 2 2 2, 1 1 1号奶牛移动至位置 4 4 4, 2 2 2号奶牛移动至位置 1 1 1;将 4 , 3 4,3 4,3号奶牛分在另一组进行循环移位,移动过后, 4 4 4号奶牛位于位置 5 5 5, 3 3 3号奶牛位于位置 3 3 3;最终完成重新排列。
每头奶牛都恰好参与一组循环移位,除非其在 A , B A,B A,B中的位置没有变化。
请计算奶牛们完成重新排列,共需多少组循环移位,最长的一组循环移位的长度是多少。
【输入格式】
第一行包含整数 N N N。
接下来 N N N行包含 A ( i ) A(i) A(i)。
再接下来 N N N行包含 B ( i ) B(i) B(i)。
【输出格式】
输出循环移位的组数,以及最长的一组循环移位的长度。
如果不存在循环移位,则第二个数输出 − 1 -1 −1。
【数据范围】
1 ≤ N ≤ 100 1≤N≤100 1≤N≤100
1 ≤ A ( i ) , B ( i ) ≤ N 1≤A(i),B(i)≤N 1≤A(i),B(i)≤N
【输入样例】
5
5
1
4
2
3
2
5
3
1
4
【输出样例】
2 3
【分析】
我们先将 A A A中的每个元素所在的位置向其在新序列 B B B中所在的位置连一条有向边,例如对于样例,我们连接这几条边: 1 → 2 , 2 → 4 , 4 → 1 , 3 → 5 , 5 → 3 1\rightarrow 2,2\rightarrow 4,4\rightarrow 1,3\rightarrow 5,5\rightarrow 3 1→2,2→4,4→1,3→5,5→3。那么就形成了两个环分别为 1 , 2 , 4 1,2,4 1,2,4和 3 , 5 3,5 3,5。这两个环也就是我们要找的两组循环移位的数所在的位置,即共有两组数需要循环移位,最长的一组循环移位的长度为 3 3 3。
【代码】
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
int a[N], idx[N], st[N];//a表示原顺序,idx[i]表示奶牛i在新顺序中所在的位置
int n, cnt, res;//cnt表示环的数量,res表示最长环的长度(均不考虑自环)
int main()
{
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
idx[x] = i;
}
for (int i = 0; i < n; i++)
{
int len = 0;
for (int j = i; !st[j]; j = idx[a[j]])
st[j] = true, len++;
if (len > 1) cnt++, res = max(res, len);
}
if (!cnt) cout << 0 << ' ' << -1 << endl;
else cout << cnt << ' ' << res << endl;
return 0;
}
边栏推荐
- PHP: seven cattle cloud upload file
- How to refund the pre-sale deposit of JD 618 in 2022? Can JD 618 deposit be refunded?
- Set SVG color
- 元宇宙链游与传统游戏的区别
- PHP get (remote) large file method record
- Leetcdoe 2037. Make each student have the minimum number of seat movements (yes, once)
- AcWing 131. The largest rectangle in the histogram (monotone stack classic application template)
- 使用cpolar远程办公(2)
- Golang start service background daemon
- XML Parsing Error: mismatched tag. Expected
猜你喜欢

2022 Taobao 618 Super Cat Games introduction 618 super cat games playing skills

Vite Basics

Index query efficiency of MySQL

SOT23(Small Outline Transistor)

Leetcode 2169. 得到 0 的操作数

AcWing 128. Editor (to effectively modify the specified position in the top stack)

Set SVG color
![Reverse analysis of Huawei housekeeper software [transfer]](/img/85/7af372808e75f8791936c59918466f.jpg)
Reverse analysis of Huawei housekeeper software [transfer]

Love and hate in the Jianghu

Malicious code analysis practice - lab03-02 DLL analysis
随机推荐
How the ArrayList collection implements ascending and descending order
tensorflow 2.x 多分类混淆矩阵及评价指标计算方法(精确率、召回率、f1分数)
Several methods of importing ThinkPHP
Telecommuting with cpolar (2)
Malicious code analysis practice - lab03-03 Exe basic dynamic analysis
PHP: Excel to get the letter header
JS string combination
PHP Apple internal purchase callback processing
Bug easily ignored by recursion
AcWing 41. Stack containing min function (monotone stack)
A few secrets - a special day
对网络库协程的思考——读brpc有感
2022-06-11:注意本文件中,graph不是邻接矩阵的含义,而是一个二部图。 在长度为N的邻接矩阵matrix中,所有的点有N个,matrix[i][j]
PHP interface generates cache and MD5 encryption uniformly
CTF freshman cup PHP deserialization question - EzPop
人脸识别pip 安装dlib库失败
InfoQ 极客传媒 15 周年庆征文|position:fixed 虚拟按键触发后无法生效问题分析及解决方案探究
What can QA do in a "de QA" project?
M-Arch(番外13)GD32L233评测-来点音乐
PHP get (remote) large file method record