Tag: Indicates that the str limit sequence output is the EFI href amp
Arranged
Time Limit: 1000MS |
|
Memory Limit: 30000K |
Total Submissions: 21268 |
|
Accepted: 8049 |
Description
Title Description:
As you know, given a positive integer n, the n number of 1 to n can constitute n! Sort these permutations in order from small to large (in dictionary order), such as n=3, List 1 2 3,1 3 2,2 1 3,2 3 1,3 1 2,3 2 16 permutations.
Task Description:
Give an arrangement, find the next k arrangement of the arrangement, if the last arrangement is encountered, then the next 1 rows are listed as the 1th arrangement, that is, arrange 1 2 3...n.
For example: n = 3,k=2 gives an arrangement of 2 3 1, the next 1 permutations are 3 1 2, and the next 2 are arranged in 3 2 1, so the answer is 3 2 1.
Input
The first line is a positive integer m, which indicates the number of test data, the following is the M-Group test data, the first row of each set of test data is 2 positive integers n (1 <= N < 1024) and K (1<=k<=64), the second row has n positive integers, is a permutation of N.
Output
For each set of input data, output one line, n number, separated by a space in the middle, indicating the input arrangement of the next K arrangement.
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]For the first few of the full arrangement, where the use of the full array function, because it is used stl,stl really slow quite a lot, constantly tle later looked at someone else's code, with a copy function to save time, can be repeated call printf time loss to fill out (after using the time saved at least 500MS!!!) And then even if you use Std::ios::sync_with_stdio (false), (iOS sync statement), scanf,printf or faster ... We'll use scanf and printf later.
#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 << endlu Sing namespace std;const int maxn = 2*1e4 + 10;const int mod = 10000;typedef long Long ll;ll num[maxn];int main () { St D::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 Arrange call full permutation function save time with the copy function using iOS Sync code scanf or faster