CodeForces 553B Kyoya and Permutation(找规律)
2015-08-21 10:55
323 查看
链接:http://codeforces.com/problemset/problem/553/B
B. Kyoya and Permutation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Let's define the permutation of length
n as an array p = [p1, p2, ..., pn] consisting ofn
distinct integers from range from 1 to
n. We say that this permutation maps value1 into the value
p1, value2 into the value
p2 and so on.
Kyota Ootori has just learned about
cyclic representation of a permutation. A
cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). Thecyclic
representation is a representation ofp as a collection of cycles forming
p. For example, permutation
p = [4, 1, 6, 2, 5, 3] has acyclic representation that looks like
(142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines thestandard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the
largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the
standard cyclic representation of[4, 1, 6, 2, 5, 3] is
(421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance,[4, 1, 6, 2, 5, 3] will become
[4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of lengthn that do not change in a list in lexicographic order.
Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integersn and
k, print the permutation that wask-th on Kyoya's list.
Input
The first line will contain two integers
n, k (1 ≤ n ≤ 50,1 ≤ k ≤ min{1018, l} wherel
is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Sample test(s)
Input
Output
Input
Output
Note
The standard cycle representation is
(1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be[1, 2, 3, 4], while the second permutation would be[1, 2, 4, 3].
题意:题意不太好理解,题目说,定义cycle为一个1-n的排列,要求从某位映射几次后又回到本身了,比如题中的 1 - 1 2 - 3 - 2 4 - 4 ,其实每个排列都是有这样的规律,如随更说个 4 1 2 3,就是一个cycle 1 - 4 - 3 - 2 - 1,题目又定义合法的cycle为每个循环节内部按从大到小排序,循环节间用从小到大排序之后得到的序列与原来一致才是合法的序列,如 (1)(32)(4)排序后还是 (1)(32)(4),所以是合法的,但4
1 2 3 排序后为4 3 2 1,与原来不一致,所以是不合法的!
好吧,题目有点绕,我先是理解错了题意。把1 2 3 4列出来,我们发现有这些是合法的
1 2 3 4
1 2 4 3
1 3 2 4
2 1 3 4
2 1 4 3
很明显,是不是,只有相邻的两位可以交换,某一位要么不交换,要么只能交换一次,用dp的思想,dp
= dp[n-1] +dp[n-2],就是每n位不交换,或都与下一位交换。这不就是斐波那契数列嘛,发现这个规律后,就可以如这一位比dp
大,则这一位就要交换,否则不用交换。代码就很简单了。
B. Kyoya and Permutation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Let's define the permutation of length
n as an array p = [p1, p2, ..., pn] consisting ofn
distinct integers from range from 1 to
n. We say that this permutation maps value1 into the value
p1, value2 into the value
p2 and so on.
Kyota Ootori has just learned about
cyclic representation of a permutation. A
cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). Thecyclic
representation is a representation ofp as a collection of cycles forming
p. For example, permutation
p = [4, 1, 6, 2, 5, 3] has acyclic representation that looks like
(142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines thestandard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the
largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the
standard cyclic representation of[4, 1, 6, 2, 5, 3] is
(421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance,[4, 1, 6, 2, 5, 3] will become
[4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of lengthn that do not change in a list in lexicographic order.
Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integersn and
k, print the permutation that wask-th on Kyoya's list.
Input
The first line will contain two integers
n, k (1 ≤ n ≤ 50,1 ≤ k ≤ min{1018, l} wherel
is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Sample test(s)
Input
4 3
Output
1 3 2 4
Input
10 1
Output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is
(1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be[1, 2, 3, 4], while the second permutation would be[1, 2, 4, 3].
题意:题意不太好理解,题目说,定义cycle为一个1-n的排列,要求从某位映射几次后又回到本身了,比如题中的 1 - 1 2 - 3 - 2 4 - 4 ,其实每个排列都是有这样的规律,如随更说个 4 1 2 3,就是一个cycle 1 - 4 - 3 - 2 - 1,题目又定义合法的cycle为每个循环节内部按从大到小排序,循环节间用从小到大排序之后得到的序列与原来一致才是合法的序列,如 (1)(32)(4)排序后还是 (1)(32)(4),所以是合法的,但4
1 2 3 排序后为4 3 2 1,与原来不一致,所以是不合法的!
好吧,题目有点绕,我先是理解错了题意。把1 2 3 4列出来,我们发现有这些是合法的
1 2 3 4
1 2 4 3
1 3 2 4
2 1 3 4
2 1 4 3
很明显,是不是,只有相邻的两位可以交换,某一位要么不交换,要么只能交换一次,用dp的思想,dp
= dp[n-1] +dp[n-2],就是每n位不交换,或都与下一位交换。这不就是斐波那契数列嘛,发现这个规律后,就可以如这一位比dp
大,则这一位就要交换,否则不用交换。代码就很简单了。
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; typedef long long ll; ll n,k; ll f[50];//<span style="font-size:18px;">斐波那契数</span>要用long long 表示 int main() { f[0]=f[1]=1; for(int i=2;i<=50;i++) f[i]=f[i-1]+f[i-2]; while(scanf("%lld %lld",&n,&k) != EOF) { for(int i=n-1;i>=0;i--) { if(k>f[i]) { printf("%lld %lld ",n-i+1,n-i); k-=f[i]; i--; } else{ printf("%lld ",n-i); } } printf("\n"); } return 0; }
相关文章推荐
- Maven构建真正的J2EE项目
- js 上一天、下一天
- Nginx负载均衡深入浅出
- 机器学习入门者学习指南(经验分享)
- 固定导航栏的实现原理
- 浅谈vim环境设置
- 显卡参数
- Spring整合Mongdb,实现基本的增,删,查,改
- Cordys 的安装服务启动失败 恢复命令
- MFC文件夹选择对话框
- hdu5410(完全背包变形)
- 解决PHP curl https时error 77(Problem with reading the SSL CA cert (path? access rights?))
- HDU 2602 Bone Collector ( DP )
- 【矩阵快速幂】 HDU 5411 CRB and Puzzle 等比
- 在SQLServer中将id重新设置为从零开始的语句
- PAT《数据结构学习与实验指导》实验项目集 2-07. 素因子分解(20) C语言
- Container With Most Water
- 搜索引擎过程研究
- Android中ListView异步加载图片错位、重复、闪烁问题分析及解决方案
- Android中继承Parcelable序列化对象需要注意的问题,记录!