OpenJ_Bailian - 1037 A decorative fence
Discription
Richard just finished building his new house. Now the only thing the house misses is a cute little wooden fence. He had no idea how to make a wooden fence, so he decided to order one. Somehow he got his hands on the ACME Fence Catalogue 2002, the ultimate resource on cute little wooden fences. After reading its preface he already knew, what makes a little wooden fence cute.
A wooden fence consists of N wooden planks, placed vertically in a row next to each other. A fence looks cute if and only if the following conditions are met:
The planks have different lengths, namely 1, 2, . . . , N plank length units.
Each plank with two neighbors is either larger than each of its neighbors or smaller than each of them. (Note that this makes the top of the fence alternately rise and fall.)
It follows, that we may uniquely describe each cute fence with N planks as a permutation a1, . . . , aN of the numbers 1, . . . ,N such that (any i; 1 < i < N) (ai − ai−1)*(ai − ai+1) > 0 and vice versa, each such permutation describes a cute fence.
It is obvious, that there are many dierent cute wooden fences made of N planks. To bring some order into their catalogue, the sales manager of ACME decided to order them in the following way: Fence A (represented by the permutation a1, . . . , aN) is in the catalogue before fence B (represented by b1, . . . , bN) if and only if there exists such i, that (any j < i) aj = bj and (ai < bi). (Also to decide, which of the two fences is earlier in the catalogue, take their corresponding permutations, find the first place on which they differ and compare the values on this place.) All the cute fences with N planks are numbered (starting from 1) in the order they appear in the catalogue. This number is called their catalogue number.
After carefully examining all the cute little wooden fences, Richard decided to order some of them. For each of them he noted the number of its planks and its catalogue number. Later, as he met his friends, he wanted to show them the fences he ordered, but he lost the catalogue somewhere. The only thing he has got are his notes. Please help him find out, how will his fences look like.
Input
The first line of the input file contains the number K (1 <= K <= 100) of input data sets. K lines follow, each of them describes one input data set.
Each of the following K lines contains two integers N and C (1 <= N <= 20), separated by a space. N is the number of planks in the fence, C is the catalogue number of the fence.
You may assume, that the total number of cute little wooden fences with 20 planks fits into a 64-bit signed integer variable (long long in C/C++, int64 in FreePascal). You may also assume that the input is correct, in particular that C is at least 1 and it doesn't exceed the number of cute fences with N planks.
Output
For each input data set output one line, describing the C-th fence with N planks in the catalogue. More precisely, if the fence is described by the permutation a1, . . . , aN, then the corresponding line of the output file should contain the numbers ai (in the correct order), separated by single spaces.
Sample Input
2
2 1
3 3
Sample Output
1 2
2 3 1 题意大概是 求字典序第C大的 波浪排列是什么。
仅有一个排列中任意一个数比两边都大或者比两边都小,那么这个排列就是波浪排列。
(恕我直言要不是C大了没法表示的话。。。这个题 N 出到 5000都可以。。。。) 预处理出波浪排列数,然后一位一位统计就行了。注意第一位的上升下降趋势是不确定的,但是当填的数值一样的时候是要优先下一步下降的(也就是这一步上升),因为这样的字典序肯定要小于填的数一样但下一步上升的排列。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=29;
ll C,f[maxn][maxn][2],g[maxn][2];
int n,a[maxn],T,L,con;
bool v[maxn]; inline void init(){
f[1][1][0]=f[1][1][1]=g[1][0]=g[1][1]=1;
for(int i=2;i<=20;i++){
for(int j=1;j<=i;j++) f[i][j][0]=g[j-1][1],f[i][j][1]=g[j][0];
for(int j=1;j<=i;j++) g[j][1]=g[j-1][1]+f[i][j][1];
for(int j=i;j;j--) g[j][0]=g[j+1][0]+f[i][j][0];
}
} inline void solve(){
memset(v,0,sizeof(v));
for(int i=1;i<=n;i++){
if(C<=f[n][i][0]){ con=0,a[1]=i,v[i]=1; break;}
else C-=f[n][i][0];
if(C<=f[n][i][1]){ con=1,a[1]=i,v[i]=1; break;}
else C-=f[n][i][1];
} for(int i=2,now=0;i<=n;now=0,i++)
for(int j=1;j<=n;j++) if(!v[j]){
now++;
if(con+(j>a[i-1])==1) continue; if(C<=f[n-i+1][now][con^1]){ con^=1,v[j]=1,a[i]=j; break;}
else C-=f[n-i+1][now][con^1];
} for(int i=1;i<=n;i++) printf("%d ",a[i]);
puts("");
} int main(){
init();
scanf("%d",&T);
while(T--) scanf("%d%lld",&n,&C),solve();
return 0;
}
OpenJ_Bailian - 1037 A decorative fence的更多相关文章
- poj 1037 A decorative fence
题目链接:http://poj.org/problem?id=1037 Description Richard just finished building his new house. Now th ...
- POJ1037 A decorative fence
题意 Language:Default A decorative fence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 84 ...
- POJ1037 A decorative fence 【动态规划】
A decorative fence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6489 Accepted: 236 ...
- A decorative fence
A decorative fence 在\(1\sim n\)的全排列\(\{a_i\}\)中,只有大小交错的(即任意一个位置i满足\(a_{i-1}<a_i>a_{i+1}ora_{i- ...
- POJ1037A decorative fence(动态规划+排序计数+好题)
http://poj.org/problem?id=1037 题意:输入木棒的个数n,其中每个木棒长度等于对应的编号,把木棒按照波浪形排序,然后输出第c个; 分析:总数为i跟木棒中第k短的木棒 就等于 ...
- POJ1037A decorative fence(好dp)
1037 带点组合的东西吧 黑书P257 其实我没看懂它写的嘛玩意儿 这题还是挺不错的 一个模糊的思路可能会好想一些 就是大体的递推方程 dp1[][]表示降序 dp2[][]表示升序 数组的含义为长 ...
- 【POJ1037】A decorative fence(DP)
BUPT2017 wintertraining(15) #6C 题意 给长度n的数列,1,2,..,n,按依次递增递减排序,求字典序第k小的排列. 题解 dp. up[i][j]表示长度为j,以第i小 ...
- poj1037 [CEOI 2002]A decorative fence 题解
---恢复内容开始--- 题意: t组数据,每组数据给出n个木棒,长度由1到n,除了两端的木棒外,每一根木棒,要么比它左右的两根都长,要么比它左右的两根都短.即要求构成的排列为波浪型.对符合要求的排列 ...
- $Poj1037\ A\ Decorative\ Fence$ 计数类$DP$
Poj AcWing Description Sol 这题很数位$DP$啊, 预处理$+$试填法 $F[i][j][k]$表示用$i$块长度不同的木板,当前木板(第$i$块)在这$i$块木板中从小到 ...
随机推荐
- 获取访问者ip及其所在城市
原本使用新浪Api,然后发现不行了,以下小编重新查找了几个,个人推荐太平洋的接口 1.首先获取真实ip $ip = $_SERVER['REMOTE_ADDR']; 2.要知道的Api接口 几个接口$ ...
- fclose后断电引起的数据丢失问题
问题背景: 客户反馈,设备断电以后,重新启动,原有配置丢失变砖 问题分析: 变砖的直接原因是配置丢失,配置丢失的原因是启动后flash上的数据已经被破坏,读取失败: 进一步分析,主要是flash数据未 ...
- JavaScript里面的正则以及eval
1.eval JavaScript中的eval是Python中eval和exec的合集,既可以编译代码也可以获取返回值. eval() EvalError 执行字符串中的JavaScript代码 ...
- jQuery选择器之元素选择器
元素选择器:根据给定(html)标记名称选择所有的元素. 描述: $('element') 搜索指定元素标签名的所有节点,这是一个合集的操作.同样的也有原生方法getElementsByTagName ...
- Rational Rose 使用技巧
1.浏览区 2.菜单项 其中Format选项中: 决定各项是否显示,也可以通过右击-option选择 3.常用快捷键: F1:任何时候都可以按F1获得相关帮助,把鼠标放在某条菜单上按F1可以获得这条菜 ...
- 【bzoj4881】[Lydsy2017年5月月赛]线段游戏 树状数组+STL-set
题目描述 quailty和tangjz正在玩一个关于线段的游戏.在平面上有n条线段,编号依次为1到n.其中第i条线段的两端点坐标分别为(0,i)和(1,p_i),其中p_1,p_2,...,p_n构成 ...
- SSWR 跟 进一法除法
1.对于浮点数SSWR float x = 3.456; //保留到小数点后两位 ) + 0.5) / 100.0; //output b = 3.46; 2.对于整数SSWR float x ; ...
- [poj] 2549 Sumsets || 双向bfs
原题 在集合里找到a+b+c=d的最大的d. 显然枚举a,b,c不行,所以将式子移项为a+b=d-c,然后双向bfs,meet int the middle. #include<cstdio&g ...
- 蓝萝卜blu netty3升netty4
老项目是netty3的,本来想直接改到netty5,但是netty5居然是只支持jdk1.7,很奇怪jdk1.6和jdk1.8都不行..为了兼容jdk1.6加上netty4本来和netty5就差别不大 ...
- hdu6188&&百度之星初赛(B) T5
度度熊的交易计划 Problem Description 度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题: 喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区. 由于生 ...