ACM: 动态规划题 poj&nb…
2016-05-19 23:26
417 查看
A decorative
fence
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.
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
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
题意: 要求按照中间木板要么都低于/高于两侧木板的排列组合, 现在要你求出第C个字典序排列序列.
解题思路: (黑书思路)
1. 问题要求出num >= ∑(A1, ... , Ak)的排列组合数, 会有2种情况,
当第一块木板A1 < A2时 ,
要求降序(A3 A2时, 要求升序(A3>A2).
设dp[i][j].up或则dp[i][j].down: 后i个项,头一块木板的高度为j的升序或降序的排列组合数.
显然有: dp[i][j].up = ∑(dp[i-1][k].down)
(j<=k<=i-1); (k范围: Ai
< Ai-1)
dp[i][j].down = ∑(dp[i-1][k].up)
(1<=k<=j-1); (k范围: Ai
> Ai-1)
因为, 向后推了一位, 原来升序/降序情况反转.
2. 接下来是一位一位确定序列, 注意要反转升序和降序选择.
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 22
struct node
{
__int64 up, down;
}g[MAX][MAX]; //g[i][j].up: 序列后i项中, 第一项高度为j, 且头2块木头上升的排列数目;
//g[i][j].down: ...., ...., ...下降...;
int n;
__int64 c;
int result[MAX];
void init()
{
memset(g, 0, sizeof(g));
g[1][1].up = g[1][1].down = 1;
for(int i = 2; i <= 20; ++i)
{
for(int j = 1; j
<= i; ++j)
{
for(int k1 =
j; k1 <= i-1; ++k1)
g[i][j].up
+= g[i-1][k1].down;
for(int k2 =
1; k2 <= j-1; ++k2)
g[i][j].down
+= g[i-1][k2].up;
}
}
}
void solve(int n, __int64 c, bool choice)
{
if(n == 0) return ;
int num;
__int64 sum = 0;
if( !choice )
{
num = result[n+1];
while(sum + g
[num].down
< c)
{
sum +=
g
[num].down;
num++;
}
}
else
{
num = 1;
while(sum + g
[num].up
< c)
{
sum +=
g
[num].up;
num++;
}
}
result
= num;
solve(n-1, c-sum, !choice);
for(int i = 1; i < n; ++i)
if(result[i] >=
num)
result[i]++;
}
int main()
{
// freopen("input.txt", "r", stdin);
init();
int caseNum, i;
scanf("%d", &caseNum);
while(caseNum--)
{
scanf("%d %I64d",
&n, &c);
int num = 1;
__int64 sum = 0;
while( (sum + g
[num].up +
g
[num].down) < c )
{
sum +=
g
[num].up + g
[num].down;
num++;
}
result
= num;
if( (sum + g
[num].down)
< c )
solve(n-1,
c-sum-g
[num].down, false);
else
solve(n-1,
c-sum, true);
for(i = 1; i <
n; ++i)
if(result[i]
>= num)
result[i]++;
for(i = n; i
>= 1; --i)
{
if(i == n)
printf("%d", result[i]);
else printf("
%d", result[i]);
}
printf("\n");
}
return 0;
}
fence
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.
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
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
题意: 要求按照中间木板要么都低于/高于两侧木板的排列组合, 现在要你求出第C个字典序排列序列.
解题思路: (黑书思路)
1. 问题要求出num >= ∑(A1, ... , Ak)的排列组合数, 会有2种情况,
当第一块木板A1 < A2时 ,
要求降序(A3 A2时, 要求升序(A3>A2).
设dp[i][j].up或则dp[i][j].down: 后i个项,头一块木板的高度为j的升序或降序的排列组合数.
显然有: dp[i][j].up = ∑(dp[i-1][k].down)
(j<=k<=i-1); (k范围: Ai
< Ai-1)
dp[i][j].down = ∑(dp[i-1][k].up)
(1<=k<=j-1); (k范围: Ai
> Ai-1)
因为, 向后推了一位, 原来升序/降序情况反转.
2. 接下来是一位一位确定序列, 注意要反转升序和降序选择.
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 22
struct node
{
__int64 up, down;
}g[MAX][MAX]; //g[i][j].up: 序列后i项中, 第一项高度为j, 且头2块木头上升的排列数目;
//g[i][j].down: ...., ...., ...下降...;
int n;
__int64 c;
int result[MAX];
void init()
{
memset(g, 0, sizeof(g));
g[1][1].up = g[1][1].down = 1;
for(int i = 2; i <= 20; ++i)
{
for(int j = 1; j
<= i; ++j)
{
for(int k1 =
j; k1 <= i-1; ++k1)
g[i][j].up
+= g[i-1][k1].down;
for(int k2 =
1; k2 <= j-1; ++k2)
g[i][j].down
+= g[i-1][k2].up;
}
}
}
void solve(int n, __int64 c, bool choice)
{
if(n == 0) return ;
int num;
__int64 sum = 0;
if( !choice )
{
num = result[n+1];
while(sum + g
[num].down
< c)
{
sum +=
g
[num].down;
num++;
}
}
else
{
num = 1;
while(sum + g
[num].up
< c)
{
sum +=
g
[num].up;
num++;
}
}
result
= num;
solve(n-1, c-sum, !choice);
for(int i = 1; i < n; ++i)
if(result[i] >=
num)
result[i]++;
}
int main()
{
// freopen("input.txt", "r", stdin);
init();
int caseNum, i;
scanf("%d", &caseNum);
while(caseNum--)
{
scanf("%d %I64d",
&n, &c);
int num = 1;
__int64 sum = 0;
while( (sum + g
[num].up +
g
[num].down) < c )
{
sum +=
g
[num].up + g
[num].down;
num++;
}
result
= num;
if( (sum + g
[num].down)
< c )
solve(n-1,
c-sum-g
[num].down, false);
else
solve(n-1,
c-sum, true);
for(i = 1; i <
n; ++i)
if(result[i]
>= num)
result[i]++;
for(i = n; i
>= 1; --i)
{
if(i == n)
printf("%d", result[i]);
else printf("
%d", result[i]);
}
printf("\n");
}
return 0;
}
相关文章推荐
- ACM: 华东师范oj 1600&n…
- ACM: 金华赛区题目 水题…
- ACM: 动态规划题 poj&nb…
- ACM: 动态规划题 《黑书…
- ACM: 动态规划题 sicily…
- ACM: 图论题 poj 1…
- ACM: 图论题 poj 1466 最大独立团
- ACM: 图论题 poj 1…
- 图论: 差分约束系统
- ACM: 图论题 poj 1275 差分约束题
- ACM: 图论题 poj 1236 强连通
- ACM: 图论题 poj 1149 网络流问题
- ACM: 图论题 poj 1201 差分约束
- ACM: 搜索题 poj 1020
- ACM: 图论题 poj 3308 最大流问题
- Android的自定义控件起步
- ACM: 图论题 poj 3177 同 3352
- dos批处理中%~dp0%的说明
- ACM: 图论题 poj 2186 强连通分量
- ACM: 博弈题 poj 1143 状态压缩