標籤:表示 str limit 順序 output 就是 efi href amp
排列
Time Limit: 1000MS |
|
Memory Limit: 30000K |
Total Submissions: 21268 |
|
Accepted: 8049 |
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
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
Source
[email protected] 求第幾個全排列,這裡使用全排列函數做的,因為是用的stl,stl確實慢了挺多,不停地tle後來看了別人的代碼,用了一種copy函數節省了時間,可以把重複調用printf的時間損失填補掉(用了之後節省了至少500MS的時間!!!)然後就是就算使用std::ios::sync_with_stdio(false);(ios同步語句),scanf,printf還是快點。。。以後都用scanf和printf好了
#include <map>#include <set>#include <stack>#include <cmath>#include <queue>#include <cstdio>#include <vector>#include <string>#include <cstring>#include <iterator>#include <iostream>#include <algorithm>#define debug(a) cout << #a << " " << a << endlusing namespace std;const int maxn = 2*1e4 + 10;const int mod = 10000;typedef long long ll;ll num[maxn];int main() { std::ios::sync_with_stdio(false); ll t; scanf("%lld",&t); while( t -- ) { ll n, m; scanf("%lld %lld", &n, &m); for( ll i = 0; i < n; i ++ ) { scanf("%lld",&num[i]); } for( ll i = 0; i < m; i ++ ) { next_permutation( num, num + n ); } copy( num, num+n-1, ostream_iterator<ll>(cout, " ")); cout << num[n-1] << endl; } return 0;}
POJ1833 排列 調用全排列函數 用copy函數節省時間 即使用了ios同步代碼scanf還是比較快