CODEFORCES Rockethon 2015 B. Permutations
来源:互联网 发布:电脑屏幕录制软件 免费 编辑:程序博客网 时间:2024/05/22 17:27
You are given a permutation p of numbers 1, 2, …, n. Let’s define f(p) as the following sum:
Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p).
Input
The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p).
The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.
In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold.In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold.
Output
Output n number forming the required permutation.
Sample test(s)
Input
2 2
Output
2 1
Input
3 2
Output
1 3 2
Note
In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
先给我们一个f(p), 求出n个数中f(p)最大的那些排列里面字典序是m的那个排列
可以发现
f(1) =1
f(2) = 2
f(3) = 4
f(4) = 8
f(5) = 16
….
所以f(n) = (2 ^ (n - 1))
而且在这些排列中,1一定在第1或者最后,2一定在第2或者倒数第二 ….. ,根据这些,我们可以递归求出排列
/************************************************************************* > File Name: B.cpp > Author: ALex > Mail: zchao1995@gmail.com > Created Time: 2015年02月09日 星期一 14时42分08秒 ************************************************************************/#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const double pi = acos(-1);const int inf = 0x3f3f3f3f;const double eps = 1e-15;typedef long long LL;typedef pair <int, int> PLL;int n, cnt;LL m;int ans[55];void work (int l, int r){ if (l == r) { ans[l] = cnt++; return; } LL tmp = (1LL << (r - l - 1)); if (tmp >= m) { ans[l] = cnt++; work (l + 1, r); } else { ans[r] = cnt++; m -= tmp; work (l, r - 1); }}int main (){ while (cin >> n >> m) { cnt = 1; work (1, n); printf("%d", ans[1]); for (int i = 2; i <= n; ++i) { printf(" %d", ans[i]); } printf("\n"); } return 0;}
- CODEFORCES Rockethon 2015 B. Permutations
- codeforces Rockethon 2015
- Codeforces Rockethon 2015
- Codeforces Rockethon 2015 解题报告
- codeforces Rockethon 2015 - A. Game (= =)
- Codeforces 124B. Permutations
- CodeForces 124B Permutations
- Codeforces 513B Permutations
- CodeForces-124B Permutations
- codeforces Rockethon 2014 A
- codeforces Rockethon 2015 C. Second price auction 概率
- Codeforces Rockethon 2014 C题
- Rockethon 2014 B. Word Folding
- codeforces 251B Playing with Permutations 模拟
- Rockethon 2015C
- codeforces 391E2 (【Codeforces Rockethon 2014】E2)
- Permutations CodeForces
- Codeforces Round #285 Div.1 B Misha and Permutations Summation
- Core Animation - 过渡动画 CATransition
- linux下调试PHP core文件
- 调用系统相机 bundle为null
- 得分(Sorce,ACM/ICPC Seoul 2005,Uva1585)
- Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLIC
- CODEFORCES Rockethon 2015 B. Permutations
- 上传视频时自动完成截取缩略图(一)
- iOS-tableview上方出现一段空白的原因及解决办法
- sqlserver 时间格式函数
- 高性能tomcat
- 文件变化监视 java.nio.file.WatchService
- iOS 检测版本更新
- Nodejs根据字符串调用对象方法
- android 开发中经常使用到的库源码地址