当前位置:网站首页>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;
}
边栏推荐
- MySQL injection load_ File common path
- Tp6+memcached configuration
- Get start date and end date for all quarters of the year
- AcWing 135. Maximum subsequence sum (prefix sum + monotone queue to find the minimum value of fixed length interval)
- 基于C#的安全聊天工具设计
- 模块8作业
- Valentina Studio Pro for Mac(mac数据库管理软件)
- Leetcode 2169. 得到 0 的操作数
- 使用cpolar远程办公(2)
- FPGA key experiment
猜你喜欢

InfoQ 极客传媒 15 周年庆征文|position:fixed 虚拟按键触发后无法生效问题分析及解决方案探究

Fiddler automatically saves the result of the specified request to a file
![Reverse analysis of Huawei housekeeper software [transfer]](/img/85/7af372808e75f8791936c59918466f.jpg)
Reverse analysis of Huawei housekeeper software [transfer]

On 3dsc theory and application of 3D shape context feature

Grid layout

Malicious code analysis practice -- using apatedns and inetsim to simulate network environment

Leetcode2154. Multiply the found value by 2 (binary search)

M-arch (fanwai 12) gd32l233 evaluation -cau encryption and decryption (tease Xiaobian)

2022京東618預售定金怎麼退?京東618定金能退嗎?

M-Arch(番外12)GD32L233评测-CAU加解密(捉弄下小编)
随机推荐
A hundred secrets and a few secrets - Caesar encryption
The most detailed explanation of the top ten levels of sqli labs platform
How to refund the pre-sale deposit of JD 618 in 2022? Can JD 618 deposit be refunded?
Malicious code analysis practice -- using apatedns and inetsim to simulate network environment
PHP wechat red packet allocation logic
重量级代理缓存服务器Squid
对网络库协程的思考——读brpc有感
Get array median
Php中redis的keys问题
Mysql5.6.24 installation free deployment method
使用cpolar远程办公(2)
MYSQL——内置函数
Get all listening events in the element
tensorflow 2.x 多分类混淆矩阵及评价指标计算方法(精确率、召回率、f1分数)
Malicious code analysis practice - lab03-03 Exe basic dynamic analysis
Don't swallow rice with vinegar! Teach you 2 moves to make the fish bones "run out" safely
PHP interface generates cache and MD5 encryption uniformly
JS obtains the time period of this week and last week (one time period is from Monday to Sunday)
2021-03-24
A few secrets - a special day