标签:成功 www ble 顺序 .com math 题目 html char
此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置。
题目链接:https://www.luogu.org/problem/show?pid=2524
前传:详见洛谷P2525
Uim成功地按照顺序将礼物送到了N个妹子的手里并维持她们的和谐。
Uim现在想知道,他最终选择的顺序是所有给N个妹子送礼顺序中、字典序第几小的。
第一行一个整数N,表示有N个数。
第二行一个整数X,表示给出的排列。
输出格式:一个整数,表示是第几小的字典序。
3 231
4
1<=N<=9
输入的排列没有空格
分析:
康托展开裸题。推荐博客
AC代码:
1 #include<cstdio> 2 #include<algorithm> 3 #include<cmath> 4 #include<cstring> 5 6 int fac[15] = {1}; 7 char a[15]; 8 int n,num[15],vis[15]; 9 10 long long solCT() 11 { 12 long long ans = 1; 13 int cnt; 14 for(int i = 1;i <= n;++ i) 15 { 16 cnt = 0; 17 for(int j = i+1;j <= n;++ j) 18 if(num[j] < num[i]) ++ cnt; 19 ans += 1LL*cnt*fac[n-i]; 20 } 21 22 return ans; 23 } 24 25 int main() 26 { 27 scanf("%d",&n); 28 for(int i = 1;i <= n;++ i) 29 fac[i] = fac[i-1]*i; 30 scanf("%s",a); 31 for(int i = 0;i < n;++ i) 32 num[i+1] = a[i]-‘0‘; 33 printf("%lld\n",solCT()); 34 return 0; 35 }
标签:成功 www ble 顺序 .com math 题目 html char
原文地址:http://www.cnblogs.com/shingen/p/7668252.html