题目描述: 
大家知道,给出正整数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 200002using 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\n",a[n-1]);}return 0;
}

View Code

转载于:https://www.cnblogs.com/Roni-i/p/7835387.html

POJ 1833 排列【STL/next_permutation】相关推荐

  1. POJ 1833 排列 (STL)

    排列 http://poj.org/problem?id=1833 Time Limit: 1000MS Memory Limit: 30000K Description 题目描述:  大家知道,给出 ...

  2. poj 1833 排列 STL 全排列公式

    排列 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15173   Accepted: 6148 Description 题 ...

  3. STL经典算法集锦之排列(next_permutation/prev_permutation

    STL经典算法集锦之排列(next_permutation/prev_permutation) 来自:CSDN博客推荐文章 | 时间:2012-05-07 14:54:09 原文链接: http:// ...

  4. 基本思想(模拟)——鸡兔同笼+校门外的树+约瑟夫问题+装箱子问题+排列【POJ 1833】

    用模拟法解决问题的基本思想是对事物进行抽象,将现实世界的事物映射成计算机所能识别的代码符号,将现实事物之间的关系映射成运算或逻辑控制流. 目录 鸡兔同笼 校门外的树 约瑟夫问题 装箱子问题 排列问题 ...

  5. STL_算法(17)_排列组合 next_permutation() perv_permutation()

    next_permutation() prev_permutation() #include<iostream> #include<algorithm> #include< ...

  6. 打印数组的排列组合/STL next_permutation函数

    在STL中,除了next_permutation外,还有一个函数prev_permutation,两者都是用来计算排列组合的函数. https://blog.csdn.net/qq_42391248/ ...

  7. C++ STL 容器元素排列之next_permutation和prev_permutation的使用示范

    //本程序示例演示next_permutation和prev_permutation #include <iostream> #include <vector> #includ ...

  8. STL/next_permutation()和prev_permutation()函数

    next_permutation()和prev_permutation()函数的使用: next_permutation()和prev_permutation()函数的头文件添加为:#include  ...

  9. C++标准库---排列元素next_permutation()prev_permutation()

    bool next_permutation(beg,end) bool prev_permutation(beg,end) next_permutation()会改变区间[beg,end)内的元素次序 ...

最新文章

  1. linux平台软件动态分析工具valgrind系列工具及其可视化
  2. WPF中的MatrixTransform
  3. 重写系统自带tabbar出现的 代理错误
  4. 【架构】分布式追踪系统设计与实现
  5. Java通过FTP服务器上传下载文件的方法
  6. 获取iOS任意线程调用堆栈(四)符号化实战
  7. apache禁止访问文件或目录执行权限、禁止运行脚本PHP文件的设置方法
  8. 扫描识别工具Dynamic Web TWAIN使用教程:单独添加/删除对象
  9. 学校新机房装系统——联想机房网络同传
  10. (论文加代码)基于deap数据集的脑电情绪识别(二分类改为八分类)
  11. 电脑PHP动画制作画板,Canvas在线画图—简单制作一个画板
  12. 艺术家艾·克里斯汀·麦克拉维·佩恩gp采访23
  13. 【h5文件读取】h5文件读取——深度学习数据集常用
  14. Oracle 、SqlServer 根据日期逐日、逐月递增累加、逐行累加
  15. 2023年最新html5cavnas新年倒计时烟花动画特效
  16. WinXP_Vista禁止限制软件使用方法
  17. 20071011听力原文
  18. 财路网每日原创推送:区块链金融为何广受互联网巨头追捧?
  19. 计算机毕业设计Java家电产品售后(源码+系统+mysql数据库+lw文档)
  20. AFDX接口测试工装研究

热门文章

  1. 简单粗暴的入门机器学习
  2. 【机器视觉】 import算子
  3. 【Qt】 XML流读写XML
  4. 【ARM】Tiny4412裸板编程之Chip ID
  5. 【Linux】一步一步学Linux——indent命令(262)
  6. Android开发文章推荐
  7. 怎么判断前轮左右的位置_如何判定汽车前面左右轮的位置?大家有什么经验?...
  8. mysql 链接慢_mysql连接非常慢的觖决办法及其它常见问题解决办法
  9. python 自动控制仿真_Python自动控制鼠标操作pymouse,python
  10. python mysql数据库操作grid控件_Python学习笔记_02:使用Tkinter连接MySQL数据库实现登陆注册功能...