Title Link: http://poj.org/problem?id=1833
Test instructions is very clear, is to find out the current arrangement of the first K-permutation.
Easily, you can use the STL's Next_permulation () function to write an answer:
#include <iostream> #include <cstdio> #include <algorithm>using namespace std;int data[1025];int main () { int n,k,m; scanf ("%d", &m); while (m--) { &NBSP;&NBSP;&NBSP;SCANF ("%d%d", &n,&k); for (int i=0 ; i<n; i++) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d", &data[i]); } while (k--) next_permutation (data,data+n); for (int i=0; i<n; i++) { printf ("%d ", Data[i]); } putchar (' \ n '); } return 0;}
However submitted, will prompt timeout ...
Some can not touch the mind, because the online AC code is basically also called K-times next_permulation write.
And then saw this article:Poj 1833 arrangement--a messy water problem
Only to understand that the original number of calls to the printf function also times out:
So, just copy it to the output buffer at once.
#include <iostream> #include <cstdio> #include <iterator> #include < Algorithm>using namespace std;int data[1025];int main () { int &NBSP;N,K,M;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d", &m); while (m--) &NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d%d", &n,&k); for (int i=0; i<n; i++) &NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d", &data[i]); } while (k--) next_permutation (data,data+n); copy (data,data+n-1,ostream_iterator<int> (cout, " ")); cout<<data[n-1]<<endl; } return 0;}
At this point AC, only 500ms, indicating that the call to printf at least 500ms of time consumed. But the same code, the use of C + + mode of submission ran 985ms, almost the line ran out.
Run ID User problem Result Memory time Language Code Length Submit time
14813838 Arclabs001 1833 Accepted 696K 500MS g++ /c14>477b 2015-10-14 17:41:22
14813836 Arclabs001 1833 Accepted 204K 985MS C + + 477B 2015-10-14 17:40:55
Beginner ACM-Combinatorial Mathematics basic topic PKU 1833