Topic Link: Click to open the link
Test instructions: The nth permutation is found from all permutations of the 1~k, and N is given by the formula.
Idea: It can be found that this formula is Cantor expansion formula (Cantor expand Encyclopedia: Click to open the link). So s[i] means that the number of the I number in the current row in the first.
So, you can use a binary + tree-like array to quickly solve, and a BC topic likeness.
See the code for details:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < string> #include <vector> #include <stack> #include <bitset> #include <cstdlib> #include < cmath> #include <set> #include <list> #include <deque> #include <map> #include <queue># Define MAX (a) > (b)? ( A):(B) #define MIN (a) < (b) ( A):(B)) using namespace Std;typedef long Long ll;const double PI = ACOs ( -1.0); const double EPS = 1e-6;const int mod = 10000 00000 + 7;const int INF = 1000000000;const int maxn = 50000 + 10;int t,n,v,m,c[maxn];int sum (int x) {int ans = 0; while (x > 0) {ans + = c[x]; X-= x &-X; } return ans; void Add (int x, int d) {while (x <= N) {c[x] + = D; x + = x &-X; }}int Main () {scanf ("%d", &t); while (t--) {scanf ("%d", &n); memset (c, 0, (n+1) *sizeof (c[0])); for (int i=1;i<=n;i++) {Add (I, 1); } for (int i=1;i<=n;i++) {scanf ("%d", &v); v++; int L = 1, r = N, m; while (R > L) {m = (L + r)/2; if (SUM (m) >= v) r = m; else L = m + 1; } printf ("%d%c", l, i = = n?) ' \ n ': '); Add (L,-1); }} return 0;}
11525-permutation (binary + tree-like array)