POJ1833 排列(next_permutation()的应用)
来源:互联网 发布:python 股票分析 教程 编辑:程序博客网 时间:2024/06/10 02:15
排列
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 20135 Accepted: 7739
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。
大家知道,给出正整数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
33 12 3 13 13 2 110 21 2 3 4 5 6 7 8 9 10
Sample Output
3 1 21 2 31 2 3 4 5 6 7 9 8 10
#include <iostream>#include<cstdio>#include<algorithm>using namespace std;int f[1025];int main(){ int T,n,k; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&k); for(int i=0;i<n;i++) scanf("%d",&f[i]); int i=0; while(true) { if(i==k)break; if(!next_permutation(f,f+n)) { for(int j=0;j<n;j++) f[j]=j+1; } i++; } printf("%d",f[0]); for(int i=1;i<n;i++) printf(" %d",f[i]); printf("\n"); } return 0;}
阅读全文
0 0
- POJ1833 排列(next_permutation()的应用)
- poj1833 排列 next_permutation函数
- POJ1833 next_permutation函数应用
- [POJ1833] 排列(STL之next_permutation)
- 排列 next_permutation的应用
- poj1833 排列(C/C++)
- POJ1833 & POJ3187 & POJ3785 next_permutation应用
- POJ1833\next_permutation()
- poj1833排序(STL)—全排列
- 全排列(next_permutation)
- next_permutation(排列问题)
- next_permutation(全排列)
- next_permutation(求全排列的库函数)
- hdu 1716 排列2(next_permutation函数应用)
- Nyoj-366 D的小L (全排列函数next_permutation的应用)
- HDU1716 next_permutation 的简单应用 排列2
- (转载)全排列next_permutation
- 全排列函数 next_permutation()
- SpringCloud(十四):Feign的demo之解决超时问题
- TEX Quotes UVA
- 51nod 1640 天气晴朗的魔法
- 数据结构总结
- 调用RadioGroup的check(id)方法,监听onCheckedChanged方法被执行多次;调用clearCheck(),onCheckedChanged方法仍被执行的解决办法
- POJ1833 排列(next_permutation()的应用)
- HDU 6096 String AC自动机(多种解法)
- MariaDB设置数据库默认编码
- IDEA创建AIDL示例
- uva 10129 Play on Words
- ARKit 获取识别到的水平面
- 关于iOS中的线程安全和使用场景
- wait()和notify()
- hdu5926Mr. Frog’s Game