排列(next_permutation)
来源:互联网 发布:房山云计算数据中心 编辑:程序博客网 时间:2024/06/04 18:55
排列
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 20384 Accepted: 7806
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
//这里选择的编译器一定是c++我之前用的是g++一直TLE…….我也不知道为啥差别会这么大
#include<iostream>#include<stdio.h>#include<algorithm>#include<string>using namespace std;int a[1050];int main(){ int ncase; scanf("%d",&ncase); while(ncase--) { int n,m; scanf("%d%d",&n,&m); for(int i =0;i<n;i++) { scanf("%d",&a[i]); } int cnt = 0; do { int i =0; for(i = 0;i<n;i++) if(a[i]!=n-i) break; if(i==n) { for(int i=0;i<n;i++) a[i] =(i+1); cnt++; } cnt++; }while(cnt <= m && 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;}
阅读全文
0 0
- 排列(next_permutation)
- STL next_permutation 全排列
- 排列 next_permutation的应用
- next_permutation全排列算法
- poj1833 排列 next_permutation函数
- next_permutation(自动全排列)
- [STL]next_permutation全排列
- (全排列) next_permutation
- poj 1833 排列 next_permutation
- next_permutation(,,) C++全排列
- 全排列(STL next_permutation)
- next_permutation(全排列算法)
- 全排列 next_permutation
- next_Permutation 全排列
- POJ1731Orders next_permutation全排列
- 全排列(next_permutation)
- 全排列与next_permutation
- 输出全排列 (next_permutation)
- [cnblogs镜像]软件的选择标准
- PMP十大知识领域理解
- SVN——版本控制工具优化篇
- 旋转元素的css标签
- 线程同步与锁(一)
- 排列(next_permutation)
- 工厂模式
- HOG的参数设置
- Random生成随机数
- [cnblogs镜像]软件选择的起点:目的与需求
- [解决]迁移到 ProxySql 以后,mysql报 SQLException: ##2800
- Apache Spark 入门简介
- Linq之隐式类型、自动属性、初始化器、匿名类
- docker容器里面安装gitlab