• POJ 1833 排列


    排列
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 13977   Accepted: 5710

    Description

    题目描述:
    大家知道,给出正整数n,则1到n这n个数可以构成n!种排列,把这些排列按照从小到大的顺序(字典顺序)列出,如n=3时,列出1 2 3,1 3 2,2 1 3,2 3 1,3 1 2,3 2 1六个排列。

    任务描述:
    给出某个排列,求出这个排列的下k个排列,如果遇到最后一个排列,则下1排列为第1个排列,即排列1 2 3…n。
    比如:n = 3,k=2 给出排列2 3 1,则它的下1个排列为3 1 2,下2个排列为3 2 1,因此答案为3 2 1。

    Input

    第一行是一个正整数m,表示测试数据的个数,下面是m组测试数据,每组测试数据第一行是2个正整数n( 1 <= n < 1024 )和k(1<=k<=64),第二行有n个正整数,是1,2 … n的一个排列。

    Output

    对于每组输入数据,输出一行,n个数,中间用空格隔开,表示输入排列的下k个排列。

    Sample Input

    3
    3 1
    2 3 1
    3 1
    3 2 1
    10 2	
    1 2 3 4 5 6 7 8 9 10
    

    Sample Output

    3 1 2
    1 2 3
    1 2 3 4 5 6 7 9 8 10
    


    题意很明确,需要注意的两点:1,如果用next_permutation()提交C++过,而G++TLE;2,即使使用了C++提交,printf(),scanf()过,cin,cout则TLE,

    第一个问题可以解释,但第二个问题就不太明白了,虽然我知道printf(),scanf()比 cin,cout 效率高,但是一般都是大数据的时候才比较明显,这个题目数据只是1024,差距应该不至于到TLE吧,这个比较困惑



    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    #include<cstdlib>
    using namespace std;
    int s[1100];
    int cmp( const void *a, const void *b )
    {
        return *(int *)a - *(int *)b;
    }
    int main()
    {
        int m,N,K;
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d%d",&N,&K);
            for(int i=0; i<N; i++)
               scanf("%d",&s[i]);
            int cnt=0;
            int flag=0;
            while(1)
            {
                do
                {
                    if(flag)
                        cnt++;
                    if(cnt==K)
                    {
                        for(int i=0; i<N; i++)
                        {
                            if(i!=N-1)
                                printf("%d ",s[i]);
                            else printf("%d\n",s[i]);;
                        }
                        break;
                    }
                    flag=1;
                }while(next_permutation(s,s+N));
                if(cnt==K)break;
                else
                    qsort(s,N,sizeof s[0],cmp);
            }
        }
    
        return 0;
    }


  • 相关阅读:
    【leetcode】366.Find Leaves of Binary Tree
    【leetcode】338 .Counting Bits
    【leetcode】419.Battleships in a Board
    【leetcode】544. Output Contest Matches
    【leetcode】496. Next Greater Element I
    ArcEngine创建ShapeFile文件2
    ArcEngine创建ShapeFile文件3
    ArcEngine创建字段集
    ArcEngine创建ShapeFile文件
    ArcEngine创建个人地理数据库
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3003896.html
Copyright © 2020-2023  润新知