cf 153.div2 D. Playing with Permutations

D. Playing with Permutations

 

     比赛的时候一直没看懂题,后来看了下,其实很简单,就是给你一个变换序列q ,有两种变换1和2,1就是令Pi=Pqi,2则相反,互为逆变换,所以只要单独求出每种变换要多少少次,即可判断出来能否成功变换。

 

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <map>
#define INF 1E9
using namespace std;
int q[100];
string aim="";
char p[105];
int i,n;
string c1(string s)
{
    for(i=0;i<n;i++)
      p[i]=s[q[i]];
    return p;
}
string c2(string s)
{
    for(i=0;i<n;i++)
      p[q[i]]=s[i];
    return p;
}
string now="",nn;
int k1,k2;
int main()
{
    int k,t,lvl;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)
    {
        scanf("%d",&q[i]);
        q[i]--;
    }
    for(i=0;i<n;i++)
    {
        scanf("%d",&t);
        aim.push_back(t);
        now.push_back(i+1);
    }
    nn=now;
    for(k1=0;k1<=k&&now!=aim;now=c1(now),k1++);
    for(k2=0;k2<=k&&nn!=aim;nn=c2(nn),k2++);
    if((k-k2)%2&&(k-k1)%2||(k>1&&k1==1&&k2==1)||!k1)cout<<"NO"<<endl;
    else cout<<"YES"<<endl;

}

 

时间: 2024-08-10 15:10:44

cf 153.div2 D. Playing with Permutations的相关文章

cf 153.div2 B. Unsorting Array

  B. Unsorting Array         最近写题目越来越少了,以至于这种简单题都会错--比赛的时候写了个n^2的算法,果断超时,赛后加了个剪枝,78ms         但是其实因为只要把有序序列中的连续不相等的两项交换,即可使之无序--所以o(n)就够了   o(n^2) /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate th

cf 167.div2 D.Dima and Two Sequences

    今天的cf完全不在状态,A题10分钟才读懂题意.目测要掉rating了     这题题意很简单,就是按X升序排列会有多少种情况,一个排列组合的问题     设相同x的元素个数为Xi  ,完全相同为2K(易知若完全相同有且仅有2个,比赛的时候就没想到这一点)     ans= ∑Xi!/2^k    之所以能在阶乘中除2^k,是因为k最大为n/2,而n!中的2的因子至少为n/2 /* author:jxy lang:C/C++ university:China,Xidian Univers

CF 203 div2 E. Wrong Floyd 图论

    题目中只选取k个点更新,因此只要保证有一个点只连到非k点即可     注意:题目要求连通图!!比赛的时候没看到,WA了,只要改下输出顺序即可保证联通. /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include

cf 154.div2 D. Table with Letters - 2

D. Table with Letters - 2         今天的比赛题比较奸诈,居然是文件读入的,让A题耽误了很多时间.           这题是dp的一个经典类型,o((n*m)^2)的算法非常好写,但是必然TLE.因为其要求四角均相等,不妨以此为条件进行判断,即可缩为o(n^2*m)算法   /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please ind

cf 167.div2 E.Dima and Horses

    题意很简单,就是分组,让冲突最多有一种,因为只要分成2组,就是个2-SAT     但是最多只有3个不和,就是不和的三个里至少有2个在一组,所以应该必然有解(这边不太明白,感觉这样),直接染色就可以了 /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #includ

cf 151.div2 C - Beauty Pageant

C - Beauty Pageant         很简单的一道题,就是给出k种不同的组合就可以了,比赛的时候没有注意到k的范围,直接dfs,果断超时,k只有(n+1)n/2种,所以可以直接枚举即可--       以后要看好范围再写     dfs代码(修改后) /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */

cf 156.div2 C Almost Arithmetical Progression

     貌似是两周前的比赛,一直忙考试没写.      C题就是找个最长的交替序列,直接模拟可以过,比赛的时候以为会超时就没写,刚写了下居然过了      也可以用dp,dp[i][j]=dp[j][last]+1,last为最后一个和i相等的数下标,就是i和last是两个相同的数,中间加一个j为任意数   模拟代码: /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,ple

cf 204 div2 D. Jeff and Furik 逆序对

     又一次看错题意--题目是两个人,一个人自己主观选择,一个人抛硬币,因为算期望,所以抛硬币那人可以无视掉,求出逆序对个数m,m为奇答案是2m-1,否则2m     太囧 #include<iostream> #include<cstdlib> #include<cstdio> #include<algorithm> using namespace std; int org[100000]; int c[100000]; int ans=0; void

cf 204 div2 C Jeff and Rounding 模拟

    智商题,如果没有0就很简单,一半加一半减,恒定的,和选择无关.有0的话就可以选择和某些配对,于是就可以更改加减次数.而枚举加减次数即可,比赛时就没想清楚这一点.具体见代码 /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio>