zoukankan      html  css  js  c++  java
  • [POJ1721]Cards

    Description
    剀剀和凡凡有N张牌(依次标号为1,2,……,N)和一台洗牌机。假设N是奇数。洗牌机的功能是进行如下的操作:对所有位置I(1≤I≤N),如果位置I上的牌是J,而且位置J上的牌是K,那么通过洗牌机后位置I上的牌将是K。
    剀剀首先写下一个1~N的排列ai,在位置ai处放上数值ai+1的牌,得到的顺序x1, x2, ..., xN作为初始顺序。他把这种顺序排列的牌放入洗牌机洗牌S次,得到牌的顺序为p1, p2, ...,pN。现在,剀剀把牌的最后顺序和洗牌次数告诉凡凡,要凡凡猜出牌的最初顺序x1, x2, ..., xN。

    Input
    第一行为整数N和S。1≤N≤1000,1≤S≤1000。第二行为牌的最终顺序p1, p2, ..., pN。

    Output
    为一行,即牌的最初顺序x1, x2, ..., xN。

    Sample Input
    5 2
    4
    1
    5
    3
    2

    Sample Output
    2
    5
    4
    1
    3


    强行暴力,经过res个操作后会回到原顺序,最后做(res-s\%res)个操作即可

    /*program from Wolfycz*/
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define inf 0x7f7f7f7f
    using namespace std;
    typedef long long ll;
    typedef unsigned int ui;
    typedef unsigned long long ull;
    inline int read(){
    	int x=0,f=1;char ch=getchar();
    	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
    	for (;ch>='0'&&ch<='9';ch=getchar())  x=(x<<1)+(x<<3)+ch-'0';
    	return x*f;
    }
    inline void print(int x){
    	if (x>=10)	 print(x/10);
    	putchar(x%10+'0');
    }
    const int N=1e3;
    int x[N+10],y[N+10],tmp[N+10];
    int n,s,res;
    void Next(){
    	memcpy(tmp,x,sizeof(x));
    	for (int i=1;i<=n;i++)   x[i]=tmp[x[i]];
    }
    bool check(){
    	for (int i=1;i<=n;i++)   if (x[i]!=y[i]) return 0;
    	return 1;
    }
    int main(){
    	n=read(),s=read();
    	for (int i=1;i<=n;i++)   x[i]=y[i]=read();
    	for (res=1;;res++){
    		Next();
    		if (check())	break;
    	}
    	res-=s%res;
    	for (;res;res--)	Next();
    	for (int i=1;i<=n;i++)   printf("%d
    ",x[i]);
    	return 0;
    }
    
  • 相关阅读:
    最短Hamilton路径-状压dp解法
    泡芙
    斗地主
    楼间跳跃
    联合权值
    虫食算
    抢掠计划
    间谍网络
    城堡the castle
    【模板】缩点
  • 原文地址:https://www.cnblogs.com/Wolfycz/p/8525092.html
Copyright © 2011-2022 走看看