[BZOJ] 3301: [USACO2011 Feb] Cow Line

康拓展开/逆展开 模板

#include<algorithm>
#include<iostream>
#include<cstdio>
#define int long long
using namespace std;
inline int rd(){
int ret=0,f=1;char c;
while(c=getchar(),!isdigit(c))f=c=='-'?-1:1;
while(isdigit(c))ret=ret*10+c-'0',c=getchar();
return ret*f;
}
#define space() putchar(' ')
#define nextline() putchar('\n')
void pot(int x){if(!x)return;pot(x/10);putchar('0'+x%10);}
void out(int x){if(!x)putchar('0');if(x<0)putchar('-'),x=-x;pot(x);}
const int MAXN = 24;
int n,m;
int fac[MAXN],a[MAXN];
int vis[MAXN],tim;
void ct(int x){
x--;
++tim;
for(int i=1;i<=n;i++){
int cur=x/fac[n-i],j;
for(j=1;j<=n;j++){
if(vis[j]==tim)continue;
if(cur==0) break;
cur--;
}
vis[j]=tim;
out(j);space();
x%=fac[n-i];
}
nextline();
}
void rct(){
int ans=1;
for(int i=1;i<=n;i++){
int tmp=0;
for(int j=i+1;j<=n;j++){
if(a[j]<a[i])tmp++;
}
ans+=fac[n-i]*tmp;
}
out(ans);nextline();
}
signed main(){
n=rd();m=rd();
fac[0]=1;
for(int i=1;i<=n;i++) fac[i]=fac[i-1]*i;
char s[MAXN];
int x;
for(int i=1;i<=m;i++){
scanf("%s",s);
if(s[0]=='P'){
x=rd();
ct(x);
}else{
for(int j=1;j<=n;j++)a[j]=rd();
rct();
}
}
return 0;
}
posted @   GhostCai  阅读(103)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示