题链;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.

A 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 251 B Playing with Permutations 暴力 分类讨论的更多相关文章

  1. 【cf789B】Masha and geometric depression(分类讨论/暴力)

    B. Masha and geometric depression 题意 在黑板上写数列,首项是b,公比是q,超过l时就停止不写.给定m个数,遇到后跳过不写.问一共写多少个数,如果无穷个输出inf. ...

  2. CF 715 E. Complete the Permutations

    CF 715 E. Complete the Permutations 题目大意:给定两个排列\(p,q\)的一部分.定义两个排列\(p,q\)的距离为使用最少的交换次数使得\(p_i=q_i\).对 ...

  3. UVaLive 6862 Triples (数学+分类讨论)

    题意:给定一个n和m,问你x^j + y^j = z^j 的数量有多少个,其中0 <= x <= y <= z <= m, j = 2, 3, 4, ... n. 析:是一个数 ...

  4. CodeForces - 789B B. Masha and geometric depression---(水坑 分类讨论)

    CodeForces - 789B 当时题意理解的有点偏差,一直wa在了14组.是q等于0的时候,b1的绝对值大于l的时候,当b1的绝对值大于l的时候就应该直接终端掉,不应该管后面的0的. 题意告诉你 ...

  5. P5979 [PA2014]Druzyny dp 分治 线段树 分类讨论 启发式合并

    LINK:Druzyny 这题研究了一下午 终于搞懂了. \(n^2\)的dp很容易得到. 考虑优化.又有大于的限制又有小于的限制这个非常难处理. 不过可以得到在限制人数上界的情况下能转移到的最远端点 ...

  6. Codeforces 1461F - Mathematical Expression(分类讨论+找性质+dp)

    现场 1 小时 44 分钟过掉此题,祭之 大力分类讨论. 如果 \(|s|=1\),那么显然所有位置都只能填上这个字符,因为你只能这么填. scanf("%d",&n);m ...

  7. Codeforces 521E - Cycling City(点双连通分量+分类讨论)

    Codeforces 题面传送门 & 洛谷题面传送门 大家都是暴力找生成树然后跳路径,代码不到 50 行(暴论)的一说--好,那本蒟蒻决定提供一种代码 150 行,但复杂度也是线性的分类讨论做 ...

  8. Codeforces 460D Little Victor and Set --分类讨论+构造

    题意:从区间[L,R]中选取不多于k个数,使这些数异或和尽量小,输出最小异或和以及选取的那些数. 解法:分类讨论. 设选取k个数. 1. k=4的时候如果区间长度>=4且L是偶数,那么可以构造四 ...

  9. BZOJ-1067 降雨量 线段树+分类讨论

    这道B题,刚的不行,各种碎点及其容易忽略,受不鸟了直接 1067: [SCOI2007]降雨量 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 2859 ...

随机推荐

  1. 【Linux】linux命令大全

    [注意]:命令[compgen -b]可以列出所有当前系统支持的命令. 109个Linux命令 目录 1       文件管理... 5 1.1          basename. 5 1.2    ...

  2. 脑科学对基金经理的八个启示 z

    脑科学对基金经理的八个启示 第一,总想要更多.人类大脑是在物资奇缺过程中进化的,所以获得任何“资源”,如食物.性.金钱等,都可以让人感觉良好,大脑也会鼓励我们继续下去. 事实上,可卡因等药物就是“绑架 ...

  3. 【ASP.NET】 中 system.math 函数使用

    1 /* 字段 */ 2 Math.E; //2.71828182845905 3 Math.PI; //3.14159265358979 4 5 /* 静态方法 */ 6 Math.Abs; //绝 ...

  4. 一个简单功能的SQL 实现

    1.假设有一张表示cj表 Name Subject Result 张三 语文 80 张三 数学 90 张三 物理 85 李四 语文 85 李四 数学 92 李四 物理 89 要求查询结果: 姓名 语文 ...

  5. Linux目录处理命令

    1 创建命令 mkdir  -p  目录名 其中 -p表示递归创建,英文为make directories td@td-Lenovo-IdeaPad-Y410P:~$ mkdir Test 上述命令在 ...

  6. 2017.5.1 使用fat jar插件来打包有引用外部jar包的项目

    如果在程序开发时用到了第三方提供的API.jar包或者其他附属资源.在导出并生成项目的jar文件时,必须将第三方的文件一并导出,否则无法正确运行. 可以使用fat jar插件,下载地址:http:// ...

  7. EffectiveJava(15)强化对象和域的不可变性

    概念: 不可变类是其实例不能被修改的类,不可变类比可变类更加易于设计 实现和使用.它们不容易出错,而且更加安全. 优点 1.不可变对象只有创建时状态. 2.不可变对象本质上是线程安全的,它们不要求同步 ...

  8. Unity里面的自动寻路(一)

    来自:http://www.narkii.com/club/forum.php?mod=viewthread&tid=269146&highlight=Unity%E9%87%8C%E ...

  9. Codeforces 463C Gargari and Bishops 题解

    题目出处: http://codeforces.com/contest/463/problem/C 感觉本题还是挺难的.须要好好总结一下. 计算对角线的公式: 1 右斜对角线,也叫主对角线的下标计算公 ...

  10. jmeter压测-负载配置

    jmeter 压测 一般压测的时间是10-15分钟 TPS:服务端每秒钟处理的请求数  越大越好 响应时间 :越短越好 并发用户数  也就是多少并发 指标给你:tps要达到多少 响应时间要达到多少 并 ...