zoukankan      html  css  js  c++  java
  • POJ 1833 排列【STL/next_permutation】

    题目描述: 
    大家知道,给出正整数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

    【代码】:
    #include <cmath>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <set>
    #include <map>
    #include <list>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <string>
    #include <vector>
    #include <numeric>
    #include <sstream>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    using namespace std;
    typedef long long ll;
    #pragma comment(linker, "/STACK:102400000,102400000")
    #define Abs(x) ((x^(x >> 31))-(x>>31))
    #define Swap(a,b) (a^=b,b^=a,a^=b)
    #define PI acos(-1.0)
    #define INF 0x3f3f3f3f
    #define EPS 1e-8
    #define MOD 1000000007
    #define max_ 10005
    #define maxn 200002
    
    using namespace std;
    int a[1200];
    int main()
    {
        int t;
        int n,k;
        scanf("%d",&t);
        while(t--)
        {
          scanf("%d %d",&n,&k);
          for(int i=0;i<n;i++)
          {
              scanf("%d",&a[i]);
          }
          for(int i=0;i<k;i++)
          {
              next_permutation(a,a+n);
          }
          for(int i=0;i<n-1;i++)
              printf("%d ",a[i]);
          printf("%d
    ",a[n-1]);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    超经典~超全的jQuery插件大全
    如何用PHP做到页面注册审核
    php实现签到功能
    php中的实用分页类
    微信小程序,超能装的实例教程
    php之 常用的 流程管理
    php之 人员的权限管理(RBAC)
    php之简单的文件管理(基本功能)
    php最新学习-----文件的操作
    关于LAMP的配置之(虚拟机的安装、创建、配置)
  • 原文地址:https://www.cnblogs.com/Roni-i/p/7835387.html
Copyright © 2011-2022 走看看