题意:有一个由1到k组成的序列,最小是1 2 … k,最大是 k k-1 … 1,给出n的计算方式,n = s0 * (k - 1)! + s1 * (k - 2)! +… + sk-1 * 0!,
给出s1…sk,输出序列里第n大的序列。
析:我们先看第一数,如果第一个数是2,那么它前面至少有(k-1)!个排列,然后1开头肯定比2要小,同理,再考虑第二个数,在考虑再二数时,
要注意把已经搞定的数去掉,所以我们用线段树进行单点更新,当然也可以用二分+数状数组。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e4 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int sum[maxn<<2]; void push_up(int rt){ sum[rt] = sum[rt<<1] + sum[rt<<1|1]; } void build(int l, int r, int rt){
if(l == r){
sum[rt] = 1; return ;
}
int m = l+r >> 1;
build(lson);
build(rson);
push_up(rt);
} int query(int x, int l, int r, int rt){
if(l == r){
sum[rt] = 0; return l;
}
int m = l+r >> 1;
int ans;
if(sum[rt<<1] >= x) ans = query(x, lson);
else ans = query(x-sum[rt<<1], rson);
push_up(rt);
return ans;
} int main(){
int T; cin >> T;
while(T--){
scanf("%d", &n);
build(1, n, 1);
for(int i = 0; i < n; ++i){
if(i) putchar(' ');
scanf("%d", &m);
printf("%d", query(m + 1, 1, n, 1));
}
printf("\n");
}
return 0;
}