POJ1833 排列 調用全排列函數 用copy函數節省時間 即使用了ios同步代碼scanf還是比較快

來源:互聯網
上載者:User

標籤:表示   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還是比較快

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.