cf 251 B Playing with Permutations 暴力 分类讨论

原创 2015年07月07日 20:50:26

题链;http://codeforces.com/problemset/problem/251/B


B. Playing with Permutations
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Petya likes permutations a lot. Recently his mom has presented him permutation q1, q2, ..., qn of length n.

permutation a of length n is a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ n), all integers there are distinct.

There is only one thing Petya likes more than permutations: playing with little Masha. As it turns out, Masha also has a permutation of length n. Petya decided to get the same permutation, whatever the cost may be. For that, he devised a game with the following rules:

  • Before the beginning of the game Petya writes permutation 1, 2, ..., n on the blackboard. After that Petya makes exactly k moves, which are described below.
  • During a move Petya tosses a coin. If the coin shows heads, he performs point 1, if the coin shows tails, he performs point 2.
    1. Let's assume that the board contains permutation p1, p2, ..., pn at the given moment. Then Petya removes the written permutation p from the board and writes another one instead: pq1, pq2, ..., pqn. In other words, Petya applies permutation q(which he has got from his mother) to permutation p.
    2. All actions are similar to point 1, except that Petya writes permutation t on the board, such that: tqi = pi for all i from 1 to n. In other words, Petya applies a permutation that is inverse to q to permutation p.

We know that after the k-th move the board contained Masha's permutation s1, s2, ..., sn. Besides, we know that throughout the game process Masha's permutation never occurred on the board before the k-th move. Note that the game has exactly k moves, that is, throughout the game the coin was tossed exactly k times.

Your task is to determine whether the described situation is possible or else state that Petya was mistaken somewhere. See samples and notes to them for a better understanding.

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 100). The second line contains n space-separated integers q1, q2, ..., qn(1 ≤ qi ≤ n) — the permutation that Petya's got as a present. The third line contains Masha's permutation s, in the similar format.

It is guaranteed that the given sequences q and s are correct permutations.

Output

If the situation that is described in the statement is possible, print "YES" (without the quotes), otherwise print "NO" (without the quotes).

Sample test(s)
input
4 1
2 3 4 1
1 2 3 4
output
NO
input
4 1
4 3 1 2
3 4 2 1
output
YES
input
4 3
4 3 1 2
3 4 2 1
output
YES
input
4 2
4 3 1 2
2 1 4 3
output
YES
input
4 1
4 3 1 2
2 1 4 3
output
NO
Note

In the first sample Masha's permutation coincides with the permutation that was written on the board before the beginning of the game. Consequently, that violates the condition that Masha's permutation never occurred on the board before k moves were performed.

In the second sample the described situation is possible, in case if after we toss a coin, we get tails.

In the third sample the possible coin tossing sequence is: heads-tails-tails.

In the fourth sample the possible coin tossing sequence is: heads-heads.


题意:感觉题目完全看不懂啊,,突然就冒出了个t。   

题意是,给q数列,和s数列。然后p数列初始为1-n。然后通过p[q[i]]=p[i],或者p[i]=p[q[i]]这两种变换,问有没有可能在k次变换后刚刚p数列为s数列。并且在这k次变换过程中,p数列不能等于s数列。p数列一开始就为s数列也不行。


做法:因为两个变换是相反的,所以可以通过两次分别两种变换来抵消。计算出p通过第一种变换要多少步可以达到s数列,然后第二种变换要多少步,然后分类讨论。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <stack>
#include <queue>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define INF 999999999
#define eps 0.00001
#define LL __int64
#define pi acos(-1.0)

int pp[1010],p[1010];
int q[1010],s[1010];
int main()
{
	int n,k;
	while(scanf("%d%d",&n,&k)!=EOF)
	{
		for(int i=1;i<=n;i++) 
			scanf("%d",&q[i]); 
		for(int i=1;i<=n;i++) 
			scanf("%d",&s[i]);  
		for(int i=1;i<=n;i++)
			pp[i]=p[i]=i; 
		int flag;
		int T1=0;
		while(T1<=k)//tou
		{
			flag=1;
			for(int i=1;i<=n;i++)
			{
				if(pp[i]!=s[i])
					flag=0;
			}
			if(flag)
				break; 
			for(int i=1;i<=n;i++)
				pp[i]=p[q[i]];
			for(int i=1;i<=n;i++)
				p[i]=pp[i];
			T1++;
		}
		for(int i=1;i<=n;i++)
			pp[i]=p[i]=i;
		int T2=0;
		while(T2<=k)//tou
		{
			flag=1;
			for(int i=1;i<=n;i++)
			{
				if(pp[i]!=s[i])
					flag=0;
			}
			if(flag)
				break; 
			for(int i=1;i<=n;i++)
				pp[q[i]]=p[i];
			for(int i=1;i<=n;i++)
				p[i]=pp[i];
			T2++;
		}
		
		if(T1==0||T2==0)//一开始就一样
			printf("NO\n");
		else if(k==1&&(T1==1||T2==1))//一步一样
			printf("YES\n");
		else if(k!=1&&T1==1&&T2==1)
			printf("NO\n");
		else if(T1==k+1&&T2==k+1)
			printf("NO\n");
		else if((T1-k)%2==0&&T1<=k)
			printf("YES\n");
		else if((T2-k)%2==0&&T2<=k)
			printf("YES\n");
		else
			printf("NO\n");  
	}
	return 0;
}
/*
4 1
2 3 4 1
1 2 3 4

*/



版权声明:本文为博主原创文章,未经博主允许不得转载。

CF #278 (Div. 2) B.(暴力枚举+推导公式+数学构造)

B. Candy Boxes time limit per test 1 second memory limit per test 256 megabytes inp...

CF - 798B. Mike and strings - 字符串+暴力

1.题目描述: B. Mike and strings time limit per test 2 seconds memory limit per...

CF#284 (Div. 2) B.(暴力)

题目链接:http://codeforces.com/contest/499/problem/B 解题思路: 开一个is变量记录每组单词最后应该输出哪个。最后每次把原来的数组暴力扫一遍即可...

CF Bayan 2015 Contest Warm Up B.(dfs+暴力)

B. Strongly Connected City time limit per test 2 seconds memory limit per test 256 megabytes...

CF#368 B. Bakery (暴力枚举)

B. Bakery time limit per test 2 seconds memory limit per test 256 megabytes input standard inp...

CF - 813B. The Golden Age - 数学+暴力+二分

1.题目描述: B. The Golden Age time limit per test 1 second memory limit per test 256 me...

CF - 812B. Sagheer, the Hausmeister - 暴力+dp

1.题目描述: B. Sagheer, the Hausmeister time limit per test 1 second memory limit per test 256 meg...

CF - 757B. Bash's Big Day - 贪心+暴力

1.题目描述: B. Bash's Big Day time limit per test 2 seconds memory limit per test 512 m...

CF 558B Amr and The Large Array(暴力法)

B. Amr and The Large Array time limit per test 1 second memory limit per test 256 megaby...

CF - 754B. Ilya and tic-tac-toe game 模拟+暴力

1.题目描述: B. Ilya and tic-tac-toe game time limit per test 2 seconds memory ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:cf 251 B Playing with Permutations 暴力 分类讨论
举报原因:
原因补充:

(最多只允许输入30个字)