• POJ 1721 CARDS(置换)


    CARDS
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 1033   Accepted: 544

    Description

    Alice and Bob have a set of N cards labelled with numbers 1 ... N (so that no two cards have the same label) and a shuffle machine. We assume that N is an odd integer.
    The shuffle machine accepts the set of cards arranged in an arbitrary order and performs the following operation of double shuffle : for all positions i, 1 <= i <= N, if the card at the position i is j and the card at the position j is k, then after the completion of the operation of double shuffle, position i will hold the card k.

    Alice and Bob play a game. Alice first writes down all the numbers from 1 to N in some random order: a1, a2, ..., aN. Then she arranges the cards so that the position ai holds the card numbered ai+1, for every 1 <= i <= N-1, while the position aN holds the card numbered a1.

    This way, cards are put in some order x1, x2, ..., xN, where xi is the card at the ith position.

    Now she sequentially performs S double shuffles using the shuffle machine described above. After that, the cards are arranged in some final order p1, p2, ..., pN which Alice reveals to Bob, together with the number S. Bob's task is to guess the order x1, x2, ..., xN in which Alice originally put the cards just before giving them to the shuffle machine.

    Input

    The first line of the input contains two integers separated by a single blank character : the odd integer N, 1 <= N <= 1000, the number of cards, and the integer S, 1 <= S <= 1000, the number of double shuffle operations.
    The following N lines describe the final order of cards after all the double shuffles have been performed such that for each i, 1 <= i <= N, the (i+1)st line of the input file contains pi (the card at the position i after all double shuffles).

    Output

    The output should contain N lines which describe the order of cards just before they were given to the shuffle machine.
    For each i, 1 <= i <= N, the ith line of the output file should contain xi (the card at the position i before the double shuffles).

    Sample Input

    7 4
    6
    3
    1
    2
    4
    7
    5
    

    Sample Output

    4
    7
    5
    6
    1
    2
    3

    Source

     
    就是利用每一个循环一定次数一定会回到原来的状态。
    /*
    POJ  1721 CARDS
    
    */
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    const int MAXN=1010;
    int a[MAXN];
    int b[MAXN];
    int c[MAXN];
    int n,m;
    int solve()
    {
        int j;
        int cnt=0;
        while(1)
        {
            for(int i=1;i<=n;i++)
               b[i]=c[c[i]];
            cnt++;
            for(j=1;j<=n;j++)
              if(b[j]!=a[j])
                break;
            if(j>n)break;
            for(int i=1;i<=n;i++)
               c[i]=b[i];
        }
        return cnt;
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&a[i]);
                c[i]=a[i];
                b[i]=a[i];
            }
            int cnt=solve();
            m%=cnt;
            m=cnt-m;
            while(m--)
            {
                for(int i=1;i<=n;i++)
                  b[i]=a[a[i]];
                for(int i=1;i<=n;i++)
                   a[i]=b[i];
            }
            for(int i=1;i<=n;i++)
              printf("%d\n",b[i]);
        }
        return 0;
    }
  • 相关阅读:
    elasticsearch 启动
    经纬度解析API
    http://t.cn/xxxxx的短链接如何生成?
    IIS+PHP上传文件大小限制和上传时间限制,iis7和iis8上传文件大小限制和上传时间限制
    WIN2003+IIS6环境SSL证书的安装
    如何创建文件名前带点的文件夹,文件夹名字带点
    解决MYSQL的错误:Got a packet bigger than 'max_allowed_packet' bytes
    php 设置临时内存和超时设置脚本最大执行时间
    谷歌地图 API 开发之获取坐标以及街道详情
    隐藏 google 地图 Logo 隐藏 百度 地图 Logo
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2670500.html
Copyright © 2020-2023  润新知