ACM模板——康托展开
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define _for(i,a,b) for(int i = (a);i < b;i ++) 5 typedef long long ll; 6 using namespace std; 7 ll a[10003]; 8 ll rnt[10003]; 9 int vis[10003]; 10 int m,n; 11 inline ll read() 12 { 13 ll ans = 0; 14 char ch = getchar(), last = ' '; 15 while(!isdigit(ch)) last = ch, ch = getchar(); 16 while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar(); 17 if(last == '-') ans = -ans; 18 return ans; 19 } 20 inline void write(ll x) 21 { 22 if(x < 0) x = -x, putchar('-'); 23 if(x >= 10) write(x / 10); 24 putchar(x % 10 + '0'); 25 } 26 int main() 27 { 28 n = read(),m = read(); 29 ll res = 0; 30 _for(i,0,n) 31 a[i] = read(); 32 33 _for(i,0,n) 34 { 35 ll tsum = 0; 36 _for(j,0,i+1) 37 if(a[j] <= a[i]) 38 tsum ++; 39 rnt[i] = a[i]-tsum; 40 } 41 rnt[n-1] += m; 42 for(int i = n-1;i > 0;i--) 43 { 44 rnt[i-1] += rnt[i]/(n-i); 45 rnt[i] %= n-i; 46 } 47 48 memset(vis,0,sizeof(vis)); 49 _for(i,0,n) 50 { 51 rnt[i] ++; 52 int j; 53 for(j = 1;j <= n;j ++) 54 { 55 if(vis[j]) 56 continue; 57 rnt[i] --; 58 if(!rnt[i]) 59 break; 60 } 61 vis[j] = 1; 62 printf("%d ",j); 63 } 64 return 0; 65 }
洛谷 P1088 火星人,代码包括康托展开,变进制数进位合法调整,康托展开逆运算