当前位置:网站首页>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;
}
边栏推荐
- 2022 JD 618 Comment rembourser le dépôt de pré - vente? Le dépôt JD 618 peut - il être remboursé?
- 使用cpolar远程办公(2)
- XML Parsing Error: mismatched tag. Expected
- Common methods of string class
- AI - face
- scanf返回值被忽略的原因及其解决方法
- AcWing 135. Maximum subsequence sum (prefix sum + monotone queue to find the minimum value of fixed length interval)
- M-Arch(番外10)GD32L233评测-SPI驱动DS1302
- A hundred secrets and a few secrets - Caesar encryption
- M-arch (fanwai 13) gd32l233 evaluation - some music
猜你喜欢

Malicious code analysis practice - use IDA pro to analyze lab05-01 dll

The most detailed explanation of the top ten levels of sqli labs platform

AcWing 128. 编辑器(对顶栈 实现序列内部指定位置高效修改)

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

Common methods of string class

Fiddler automatically saves the result of the specified request to a file

AcWing 135. 最大子序和(前缀和 + 单调队列求定长区间最小值)
![[machine learning] practice of logistic regression classification based on Iris data set](/img/c6/0233545d917691b8336f30707e4636.png)
[machine learning] practice of logistic regression classification based on Iris data set

Class. Forname connection MySQL driver keeps throwing classnotfoundexception exception solution

Malicious code analysis practice - lab03-01 Exe basic dynamic analysis
随机推荐
Malicious code analysis practice - lab03-01 Exe basic dynamic analysis
Common methods of string class
Leetcode2154. 将找到的值乘以 2(二分查找)
^33 variable promotion and function promotion interview questions
FPGA开发——Hello_world例程
Index query efficiency of MySQL
浅谈三维形状上下文特征3DSC理论及应用
Assign a specified amount to a specified number of people at random
AcWing 1912. 里程表(枚举)
Building 64 bit wampserver and DVWA in win7 virtual machine
Sendmail Dovecot 邮件服务器
Amélioration de la 3dsc par HSC
Common configuration commands for Cisco network device security management
力扣(LeetCode)162. 寻找峰值(2022.06.11)
Create vite source code analysis
PHP get (remote) large file method record
Set SVG color
基于C#的安全聊天工具设计
Using the echart plug-in to dynamically refresh charts in uview/uni-app
The solution of Lenovo notebook ThinkPad t440 WiFi dropping all the time