NYOJ139我排第几个
2016-04-08 20:31
429 查看
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=139
康托展开:
公式神马的我都看不懂,但我会用!
举个栗子, 1324 在{1,2,3,4}这个排列中是第几个小的,先看第1位,比1小的有0个,所以有0 * 3!,第2位,比3小的有1个2(3前面的不用看),所以有1 * 2!个,在看第3位,比2小的是0个,所以 0 * 1!,第4位就不用看了。。。所以 0*3! + 1 * 2! + 0 * 1! = 2,所以1324是第三小的数。。
代码:
康托展开:
公式神马的我都看不懂,但我会用!
举个栗子, 1324 在{1,2,3,4}这个排列中是第几个小的,先看第1位,比1小的有0个,所以有0 * 3!,第2位,比3小的有1个2(3前面的不用看),所以有1 * 2!个,在看第3位,比2小的是0个,所以 0 * 1!,第4位就不用看了。。。所以 0*3! + 1 * 2! + 0 * 1! = 2,所以1324是第三小的数。。
代码:
#include <cstdio> #include <cstring> int n; char s[20]; int fac[] = {1,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600}; int main() { while(~scanf("%d",&n)) { while(n--) { long long ans = 0; scanf("%s",s); int len = strlen(s); int t = 11; for(int i = 0;i < len;++i) { int cnt = 0; for(int j = i + 1;j < len;++j) { if(s[j] < s[i]) cnt++; } ans += cnt * fac[t--]; } printf("%lld\n",ans + 1); } } return 0; }
相关文章推荐
- JS中三种主要方法(函数定义)类别理解 —(JS面向对象&原型)
- linux下启动和关闭网卡命令及DHCP上网
- 复利计算--结对1.0,做汉堡,结对2.0
- unity源码解析Material
- 2016蓝桥杯B组——NO.5
- yii小物件前端表单集合
- Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.ExtendedProperties
- Scala集合类型详解
- 【iOS开发】UITableView Cell 自适应高度
- 【蓝桥杯】历届试题 幸运数
- spring aop两种配置方式(1)
- 我需要什么样的浏览器?
- error: expected class-name before '{' token
- Windows API 中的SetThreadPriority()
- 关于java的枚举
- 最新资源分享
- JAVA基础(五)IO(三)包含装饰者设计模式
- hdu 1039 Easier Done Than Said?
- HDU 5655 CA Loves Stick 水题
- 2-jQuery - AJAX load() 方法【进阶篇】