You can see that action 1 and Operation 2 are a pair of reciprocal operations
That is, one operation 1 and one operation 2 after execution, the arrangement is unchanged
If an operation is done continuously I can get s
If I is exactly equal to K,puts ("YES")
If not, see if the remaining k-i operations can be done with one operation 1 and one operation 2
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace Std;
const int MAXN = 110;
int P_H[MAXN][MAXN];
int P_T[MAXN][MAXN];
int S[MAXN];
int Q[MAXN];
int n, K;
int T[MAXN];
int main ()
{
Freopen ("Input.txt", "R", stdin);
while (~SCANF ("%d%d", &n, &k))
{
for (int i = 1;i <= n;i++)
scanf ("%d", &q[i]), t[q[i]] = i;
int flag = 0;
for (int i = 1;i <= n;i++)
{
scanf ("%d", &s[i]);
if (s[i]! = i)
flag = 1;
}
if (!flag)
{
Puts ("NO");
Continue;
}
for (int i = 1;i <= n;i++)
P_h[0][i] = i,p_t[0][i] = i;
int pos_h = 0, pos_t = 0;
for (int i = 1;i <= k;i++)
{
int flag_h = 0, flag_t = 0;
for (int j = 1;j <= n;j++)
{
P_H[I][J] = p_h[i-1][q[j]];
if (p_h[i][j]! = s[j]) Flag_h = 1;
P_T[I][J] = p_t[i-1][t[j]];
if (p_t[i][j]! = s[j]) flag_t = 1;
}
if (!flag_h &&!pos_h) pos_h = i;
if (!flag_t &&!pos_t) pos_t = i;
}
if (Pos_h = = 1&&pos_t==1&&k > 1)
Puts ("NO");
else if (((k-pos_h)%2 = = 0&&pos_h) | | ((k-pos_t)%2 ==0&&pos_t))
Puts ("YES");
Else puts ("NO");
}
return 0;
}
Codeforces Round #153 (Div. 1) bplaying with permutations