您的位置:首页 > Web前端

poj 1037 A decorative fence ( dp+输出第k字典序)

2014-03-18 20:56 399 查看
A decorative fence

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6357 Accepted: 2313
Description

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 di erent 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抰 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

Source

CEOI 2002
题意:

给一个n,求出1~n组成波浪形按字典序的第m种序列。

思路:

首先是dp,dp[i][j][k] - 总共 i 个数以相对位置为 j 开头的序列上升(1)或下降(0)的序列个数。

dp[i][j][0]=∑dp[i-1][k][1]   1<=k<j ;

dp[i][j][1]=∑dp[i-1][k][0]    j<=k<i  (原来的序列都+1,然后将j放入首位)

dp预处理之后就是从高位依次枚举确定每个位数是几了。

从前到后枚举求得每一位:

枚举一位时,计算在这样的前缀下,后面的总的排列数。如果严格小于总编号,则该位偏小,换更大的数,同时更新总编号;若大于等于,则该位恰好,枚举下一位,总编号不用更新。

先抛开此题,用最简单的全排列问题来说明这种方法。

如1,2,3,4的全排列,共有4!种,求第10个的排列是?

先试首位是1,后234有3!=6种<10,说明1偏小,转化成以2开头的第(10-6=4)个排列,而3!=6 >= 4,说明首位恰是2。第二位先试1(1没用过),后面2!=2个<4,1偏小,换成3(2用过了)为第二位,总编号也再减去2!,剩下2了。而此时2!>=2,说明第二位恰好是3。第三位先试1,但后面1!<2,因此改用4。末位则是1了。

这样得出,第10个排列是2-3-4-1。(从1计起) 

于是这题也能按照这种方法求解了,注意本题的序列是波浪形。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 10005
#define MAXN 31290
#define OO (1<<31)-1
#define mod 100000000
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

ll n,m,ans,cnt,tot,flag;
ll dp[25][25][2];
bool vis[25];

int main()
{
ll i,j,k,s,t;
memset(dp,0,sizeof(dp));
dp[1][1][0]=dp[1][1][1]=1;
for(i=2; i<=20; i++)
{
for(j=1; j<=i; j++)
{
for(k=1; k<j; k++)
{
dp[i][j][0]+=dp[i-1][k][1];
}
for(k=j; k<=i-1; k++)
{
dp[i][j][1]+=dp[i-1][k][0];
}
}
}
cin>>t;
while(t--)
{
cin>>n>>m;
vector<int>res;
memset(vis,0,sizeof(vis));
flag=0;
for(j=1; j<=n&&!flag; j++) // 确定第一位
{
for(k=0; k<=1; k++) // up or down
{
if(m-dp
[j][k]<=0)  // 第一位为 j 状态为 k
{
res.push_back(j);
vis[j]=flag=1;
break ;
}
else m-=dp
[j][k];
}
}
for(i=1; i<n; i++) // 找第i位
{
if(!k) // down
{
cnt=0;  // 相对位置
for(j=1; j<res[i-1]; j++) // 枚举该位的数
{
if(vis[j]) continue ;
cnt++;
if(m<=dp[n-i][cnt][k^1])
{
res.push_back(j);
vis[j]=1;
break ;
}
else m-=dp[n-i][cnt][k^1];
}
}
else
{
cnt=0;  // 相对位置
for(j=1; j<res[i-1]; j++)
{
if(!vis[j]) cnt++;
}
for(j=res[i-1]+1; j<=n; j++)
{
if(vis[j]) continue ;
cnt++;
if(m<=dp[n-i][cnt][k^1])
{
res.push_back(j);
vis[j]=1;
break ;
}
else m-=dp[n-i][cnt][k^1];
}
}
k^=1;
}
for(i=0;i<res.size();i++)
{
if(i!=0) printf(" ");
printf("%d",res[i]);
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: