Codeforces Round #153 (Div. 1) bplaying with permutations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.