rockethon2015 B题 Permutations 规律+构造
来源:互联网 发布:淘宝长尾词是什么意思 编辑:程序博客网 时间:2024/05/18 03:35
题意:求使所给公式值最大的第m个排列。
思路:假设已知使f(p)最大的n-1的排列,那么对于使f(p)最大的n的排列,把n放在(n-1)两边均可。因为n放在(n-1)两边,增值为
1+2+...+n,而如果不放在两边,(n-1)到n之间的值<n-1,那么增值一定小于(n+1)n/2。接下来枚举1-->n摆放位置,对于i,如果m<2^(n-i-1),
那么i摆在pos,不然放到可放到的最后面,即last位置。因为接下来(i+1)一定在i左边,而之后比(i+1)大也一定在i左边。详见代码:
/********************************************************* file name: B.cpp author : kereo create time: 2015年02月08日 星期日 21时45分31秒*********************************************************/#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<set>#include<map>#include<vector>#include<stack>#include<cmath>#include<string>#include<algorithm>using namespace std;typedef long long ll;const int sigma_size=26;const int N=50+10;const int MAXN=100000+50;const int inf=0x3fffffff;const double eps=1e-8;const int mod=1000000000+7;#define L(x) (x<<1)#define R(x) (x<<1|1)#define PII pair<int, int>#define mk(x,y) make_pair((x),(y))int n;ll m;int ans[N];ll base[N];int main(){ base[0]=1; for(int i=1;i<N;i++) base[i]=base[i-1]*2; while(~scanf("%d%lld",&n,&m)){ int pos=1,last=n; for(int i=1;i<=n;i++){ ll tmp=base[n-1-i]; if(m<=tmp) ans[pos++]=i; else{ m-=tmp; ans[last--]=i; } } for(int i=1;i<=n;i++){ if(i == 1) printf("%d",ans[i]); else printf(" %d",ans[i]); } printf("\n"); }return 0;}
0 0
- rockethon2015 B题 Permutations 规律+构造
- CodeForces 477B-E - Dreamoon and Sets-构造规律题
- rockethon2015 G2题 Inversions problem 概率dp
- codeforces 553B Kyoya and Permutation 构造+规律
- rockethon2015 C题 Second price auction 概率dp
- Codeforces 124B. Permutations
- CodeForces 124B Permutations
- Codeforces 513B Permutations
- CodeForces-124B Permutations
- Codeforces 480B Long Jumps 规律题
- FJU2147A-B Game(找规律题)
- 问题 B: Divisible by 3 规律题
- Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)(B)(构造,找规律)
- CODEFORCES Rockethon 2015 B. Permutations
- BC #65(B) 规律
- 找规律 B
- Codeforces 396B On Sum of Fractions 规律题
- 2014牡丹江网络预选赛B题(找规律)zoj3810
- uva 10815 set的用法
- c中malloc calloc realloc 的区别和联系
- 乱码的艺术
- IO 相关指标
- Kruskal最小生成树算法
- rockethon2015 B题 Permutations 规律+构造
- Scala ListBuffer使用备忘
- Apache Storm提交Topology时的默认CLASSPATH问题
- ClassNotFoundException when unmarshalling 问题总结
- Hibernate自动生成实体类注解
- Oracle 11.2.0.1在xp上的静默(slient)安装
- android学习笔记(一)数据存储与访问
- 微信企业号终于来了 第一时间带你体验真面目
- 在使用get传递中文参数的编码问题