zoukankan      html  css  js  c++  java
  • BZOJ2214[Poi2011]Shift——模拟

    题目描述

    Byteasar bought his son Bytie a set of blocks numbered from to and arranged them in a row in a certain order. Bytie's goal is to rearrange the blocks so that they are ordered naturally, from the smallest number to the largest. However, the only moves Bytie is allowed to make are: putting the last block at the very beginning (move a), and putting the third block at the very beginning (move b). Help Bytie by writing a program that tells whether a given arrangement of blocks can be properly reordered, and tells the right sequence of moves if it is.

    有一个1..n的排列,有两种操作:
    (a) 将最后一个数移到最前面
    (b) 把第三个数移到最前面

    我们将连续进行k次同一个操作称为“一块操作”,表示为ka或kb。
    找到一个操作序列使得进行这些操作后,排列变为1,2,3,...,n。

    输入

    In the first line of the standard input there is a single integer ,(1<=N<=2000). In the second line there are integers from the range to , separated by single spaces. No number appears twice, and thus they represent the initial arrangement of the blocks. .

    第一行n(1<=n<=2000)
    下面一行n个数表示这个排列。

    输出

     

    如果不存在这样的操作序列,输出"NIE DA SIE",否则
    第一行m,表示操作的块数。
    下面一行,表示这m块操作。
    需要满足相邻两块操作的种类不同,每块操作中进行的次数大于0小于n。
    需要满足m<=n*n

    样例输入

    Sample Input #1
    4
    1 3 2 4
    Sample Output #1
    4
    3a 2b 2a 2b

    Sample Input #2
    7
    1 3 2 4 5 6 7
    Sample Output #2
    NIE DA SIE

    Sample Input #3
    3
    1 2 3
    Sample Output #3
    0
     
      虽然是一道模拟题,但思维含量还是很大的。首先,为了让序列有序,我们一定是一个一个使他们变得有序,也就是在1-i有序后使i+1排在i的后面。假设现在1~i-1已经有序了,那么就要先把i挪到到第一位,然后使这时的1~i-1在最后,这样再把1~i-1挪到最前面就使1~i有序了。但当i在最前面时,1~i-1后面会还有一些数,这时我们只要两次a一次b就可以把最后两个挪到i的后面且使i还在第一个。但这样挪完后面可能还剩一个,这时只要一次a两次b就好了。但当按上述操作进行到最后时n和n-1可能是反的,这时把n-1挪到第一个,但不能依靠上面的操作再进行了。我们可以进行两次b操作来使n-1往后挪两位,再将n-1及后面数依次挪到第一个,重复上述操作,如果n是偶数这样做就可以把序列变得有序,如果n是奇数就只能无解了。
    #include<map>
    #include<set>
    #include<queue>
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int n;
    int now;
    int tot;
    int f[2010];
    int a[2010];
    int p[2010];
    int c[4000010];
    bool vis[4000010];
    int pos(int x)
    {
        return (now+p[x]-1)%n+1;
    }
    int get(int x)
    {
        return ((x-now-1)%n+n)%n+1;
    }
    void make1(int x)
    {
        x%=n;
        if(!x)
        {
            return ;
        }
        now+=x;
        c[++tot]=x;
        vis[tot]=0;
    }
    void make2()
    {
        int s1=get(1);
        int s2=get(2);
        int s3=get(3);
        int x=f[s1];
        int y=f[s2];
        int z=f[s3];
        int t=p[x];
        p[x]=p[y];
        p[y]=p[z];
        p[z]=t;
        f[p[x]]=x;
        f[p[y]]=y;
        f[p[z]]=z;
        c[++tot]=1;
        vis[tot]=1;
    }
    void print()
    {
        int ans=0;
        for(int i=1;i<=tot;i++)
        {
            if(i==1||vis[i]!=vis[ans])
            {
                ans++;
                vis[ans]=vis[i];
                c[ans]=c[i];
            }
            else
            {
                c[ans]+=c[i];
            }
        }
        tot=ans;
        ans=0;
        for(int i=1;i<=tot;i++)
        {
            if(vis[i]==0)
            {
                c[i]%=n;
            }
            else
            {
                c[i]%=3;
            }
            if(c[i])
            {
                c[++ans]=c[i];
                vis[ans]=vis[i];
            }
        }
        printf("%d
    ",ans);
        int i;
        for(i=1;i<ans;i++)
        {
            if(vis[i]==0)
            {
                printf("%da ",c[i]);
            }
            else
            {
                printf("%db ",c[i]);
            }
        }
        if(ans)
        {
            if(vis[i]==0)
            {
                printf("%da
    ",c[i]);
            }
            else
            {
                printf("%db
    ",c[i]);
            }
        }
    }
    int main()
    {
        scanf("%d",&n);
        if(n==1)
        {
            printf("0
    ");
            return 0;
        }
        if(n==2)
        {
            scanf("%d%d",&a[1],&a[2]);
            if(a[1]==1)
            {
                printf("0
    ");
                return 0;
            }
            else
            {
                printf("1
    1a
    ");
                return 0;
            }
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            p[a[i]]=i;
            f[i]=a[i];
        }
        for(int i=2;i<=n-2;i++)
        {
            int x=pos(i);
            make1(n-x+1);
            int t=n-pos(i-1);
            while(t>1)
            {
                t-=2;
                make1(2);
                make2();
            }
            if(t)
            {
                make1(1);
                make2();
                make2();
            }
        }
        if(n==3)
        {
            make1(n-pos(1)+1);
        }
        if(f[get(2)]<f[get(3)])
        {
            make1(n-3);
            print();
        }
        else
        {
            if(!(n%2))
            {
                make1(n-2);
                for(int i=1;i<n/2;i++)
                {
                    make2();
                    make2();
                    make1(n-2);
                }
                make1(n-2);
                print();
            }
            else
            {
                printf("NIE DA SIE
    ");
            }
        }
    }
  • 相关阅读:
    JVM内存结构
    JVM中的类加载
    数据库索引详解
    Spring IOC 总结
    Java多线程(一)—— 常见问题整理
    Java集合框架 面试问题整理
    Java8 Lambda表达式
    vue开发技巧
    mysql(MariaDB)问题处理
    字符串非空判断的效率问题
  • 原文地址:https://www.cnblogs.com/Khada-Jhin/p/9589990.html
Copyright © 2011-2022 走看看