当前位置:网站首页>AcWing 1921. Rearranging cows (ring diagram)
AcWing 1921. Rearranging cows (ring diagram)
2022-06-12 10:55:00 【Eurya song】
【 Title Description 】
Farmer John's N N N First milk steak in a row , Number 1 ∼ N 1\sim N 1∼N.
They can be sorted by an array A A A To describe , among A ( i ) A(i) A(i) Is in position i i i The number of your cow .
John wants to rearrange them into a different order .
The new order uses an array B B B To describe , among B ( i ) B(i) B(i) Is in position i i i The number of your cow .
Suppose the order of cows at the beginning is :
A = 5 1 4 2 3
And suppose John wants them rearranged to :
B = 2 5 3 1 4
In order to learn from A A A Rearrange the order to B B B The order , The cows did a lot of “ loop ” displacement .
The so-called cyclic shift , It refers to the selection of several cows in the arrangement into a group , The cows in the group were cycled to move their positions , That is, the first cow moves to the position of the second cow , The second cow moves to the position of the third cow , And so on , The last cow moves to the position of the first cow .
The above case, , take 5 , 1 , 2 5,1,2 5,1,2 Cows were divided into a group for cyclic displacement , After moving , 5 5 5 Cow No. 1 moves to position 2 2 2, 1 1 1 Cow No. 1 moves to position 4 4 4, 2 2 2 Cow No. 1 moves to position 1 1 1; take 4 , 3 4,3 4,3 Cows were divided into another group for cyclic displacement , After moving , 4 4 4 Cow number one is in position 5 5 5, 3 3 3 Cow number one is in position 3 3 3; Finally complete the rearrangement .
Each cow happens to be involved in a set of cyclic shifts , Unless it is A , B A,B A,B The position in the has not changed .
Please count the cows to complete the rearrangement , How many sets of cyclic shifts are needed , What is the length of the longest set of cyclic shifts .
【 Input format 】
The first line contains integers N N N.
Next N N N Line inclusion A ( i ) A(i) A(i).
Next N N N Line inclusion B ( i ) B(i) B(i).
【 Output format 】
Number of groups of output cyclic shift , And the length of the longest set of cyclic shifts .
If there is no cyclic shift , Then the second number outputs − 1 -1 −1.
【 Data range 】
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
【 sample input 】
5
5
1
4
2
3
2
5
3
1
4
【 sample output 】
2 3
【 analysis 】
We will first A A A The position of each element in the new sequence B B B The position of the middle is connected with a directed edge , For example, for the example , We connect these sides : 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. Then two rings are formed 1 , 2 , 4 1,2,4 1,2,4 and 3 , 5 3,5 3,5. These two rings are the positions of the numbers of the two sets of cyclic shifts we are looking for , That is, there are two sets of numbers that need to be shifted circularly , The length of the longest set of cyclic shifts is 3 3 3.
【 Code 】
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
int a[N], idx[N], st[N];//a Indicates the original order ,idx[i] A cow i Position in the new sequence
int n, cnt, res;//cnt Indicates the number of rings ,res Indicates the length of the longest ring ( Do not consider self rings )
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;
}
边栏推荐
- Fiddler automatically saves the result of the specified request to a file
- file_ get_ Contents() JSON after reading_ Decode cannot be converted to array
- What can QA do in a "de QA" project?
- 浅谈三维形状上下文特征3DSC理论及应用
- Timers in golang
- PHP get (remote) large file method record
- Grid layout
- Error during session start; please check your PHP and/or webserver log file and configure your PHP
- Redis keys in PHP
- Reading mysql45 lecture - self summary (part)
猜你喜欢

Bug easily ignored by recursion

Common methods of string class

Telecommuting with cpolar (2)

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

Module 8 job

Is the acceptance standard a test case?

890. 查找和替换模式

深度学习与CV教程(14) | 图像分割 (FCN,SegNet,U-Net,PSPNet,DeepLab,RefineNet)

ArrayList automatic capacity expansion principle

AcWing 135. Maximum subsequence sum (prefix sum + monotone queue to find the minimum value of fixed length interval)
随机推荐
Index query efficiency of MySQL
模块8作业
(三十七)Bee如何同时使用不同数据源实例
Malicious code analysis practice - lab03-03 Exe basic dynamic analysis
Machine learning is not something you can use if you want to use it
Malicious code analysis practice - lab03-01 Exe basic dynamic analysis
Leetcdoe 2037. 使每位学生都有座位的最少移动次数(可以,一次过)
PHP Apple internal purchase callback processing
AcWing 132. Group queue (queue simulation question)
AcWing 1995. 见面与问候(模拟)
Malicious code analysis practice -- using apatedns and inetsim to simulate network environment
Grid layout
Vite Basics
^33变量提升和函数提升面试题
2021-03-24
A hundred secrets and a few secrets - Caesar encryption
Why check the @nonnull annotation at run time- Why @Nonnull annotation checked at runtime?
Leetcode 2169. 得到 0 的操作数
Vscode code debugging skills
Global and local existence of array, integer and character variables