1037 A decorative fence

原创 2007年10月15日 08:41:00
A decorative fence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 1741   Accepted: 503

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.

 

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

 

Sample Output

 

Source
CEOI 2002

 **************************************************************************************************

***************************************************************************************************

Source Code
#include <stdio.h>
 #include <string.h>
 
 const int N = 21;
 __int64 dp[2][N][N][N];
 int n;
 __int64 idx;
 bool chk[N];
 
void pre() {
    int i, j, m;

    memset(dp, 0, sizeof(dp));

    dp[0][1][0][0] = 1;
    dp[1][1][0][0] = 1;

   for(i = 2; i <= 20; ++i) {
     for(j = 0; j < i; ++j) {
           int k = i - j - 1;
           for(m = 1; m <= j; ++m) 
              dp[0][i][j][k] += dp[1][i-1][j-m][k+m-1];
            for(m = 1; m <= k; ++m)
                dp[1][i][j][k] += dp[0][i-1][j+m-1][k-m];
        }
    }
}

void DFS(int now, int last, int style, __int64 idx) {
    if(now <= 0) return;
    int i, j;
   for(i = 0; i < n; ++i) if(!chk[i]) {
       if(style == 0 && i < last) continue;
        if(style == 1 && i > last) return;

        chk[i] = true;
       int big = 0, small = 0;
        for(j = 0; j < n; ++j) if(!chk[j]) {
           if(j < i) small++;
            if(j > i) big++;
        }

        if(style == 0 || style == -1) {
            if(idx > dp[1][now][big][small]) idx -= dp[1][now][big][small];
            else {
                printf("%d ", i+1);
               DFS(now-1, i, 1, idx);    return;
            }
        }

        if(style == 1 || style == -1) {
            if(idx > dp[0][now][big][small]) idx -= dp[0][now][big][small];
            else {
                printf("%d ", i+1);
                DFS(now-1, i, 0, idx);    return;
           }
        }
        chk[i] = false;
   }
}

 int main() {
    int ntc;
    pre();
    scanf("%d ", &ntc);
    while(ntc--) {
        scanf("%d %I64d", &n, &idx);
        memset(chk, false, sizeof(chk));
        DFS(n, -1, -1, idx);
        putchar('/n');
    }
    return 0;
}
1 2
2 3 1
2
2 1
3 3

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.

CUDA中__threadfence()的含义与理解

在CUDA里面,不同线程间的数据读写会彼此影响,这种影响的作用效果根据不同的线程组织单位和不同的读写对象是不同。在不考虑2.x的优化的情况下,(1)在同一个warp内的线程读写shared/globa...
  • Augusdi
  • Augusdi
  • 2013年09月30日 19:40
  • 1802

关于CUDA中__threadfence的理解

__threadfence函数是memory fence函数,用来保证线程间数据通信的可靠性。与同步函数不同,memory fence不能保证所有线程运行到同一位置,只保证执行memory fence...
  • yutianzuijin
  • yutianzuijin
  • 2013年01月15日 21:20
  • 5260

PAT(BASIC)1037. 在霍格沃茨找零钱(20)

代码确实有点冗长,请见谅。#include struct money{ int Galleon; int Sickle; int Kunt; }; int main(void){ using ...
  • txdzdlxx
  • txdzdlxx
  • 2015年04月04日 10:59
  • 1045

POJ1037:A decorative fence(DP)

Description Richard just finished building his new house. Now the only thing the house misses is ...
  • libin56842
  • libin56842
  • 2015年05月13日 22:20
  • 1328

poj 1037 A decorative fence

题目链接:http://poj.org/problem?id=1037 这道题的解析见黑书257页,后面的答案搜索花了我不少时间,后来想到了从后向前还原的方法,还是相当不错的。 #includ...
  • Wings_of_Liberty
  • Wings_of_Liberty
  • 2012年03月16日 13:15
  • 366

Poj1037 A decorative fence(DP好题)

题目链接:http://poj.org/problem?id=1037 题意:给你N个板子,每个板子长度都不一样。长度为1~n,使板子排列成波浪形,即对于对于1#include #include ...
  • Hawkeye_z
  • Hawkeye_z
  • 2016年03月23日 16:29
  • 316

POJ -1037-A decorative fence(动归 +计数)

Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7637 Accepted: 2905Description ...
  • jsmok_xingkong
  • jsmok_xingkong
  • 2017年04月21日 20:31
  • 360

POJ 1037 A decorative fence(美丽栅栏)__动态规划

DescriptionRichard just finished building his new house. Now the only thing the house misses is a cu...
  • tp7309
  • tp7309
  • 2016年12月11日 01:16
  • 273

poj 1037 A decorative fence dp+序列构造

//poj 1037 //sep9 #include using namespace std; typedef long long ll; ll dp[32][32][2]; bool vis[32...
  • sepNINE
  • sepNINE
  • 2016年04月26日 17:53
  • 505

POJ1037————A decorative fence(动态规划)

A decorative fence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7545...
  • say_c_box
  • say_c_box
  • 2017年03月04日 10:43
  • 209
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:1037 A decorative fence
举报原因:
原因补充:

(最多只允许输入30个字)