[POJ 1037] A decorative fence

Posted evenbao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[POJ 1037] A decorative fence相关的知识,希望对你有一定的参考价值。

[题目链接]

          http://poj.org/problem?id=1037

[算法]

         DP

[代码]

        

#include <algorithm>  
#include <bitset>  
#include <cctype>  
#include <cerrno>  
#include <clocale>  
#include <cmath>  
#include <complex>  
#include <cstdio>  
#include <cstdlib>  
#include <cstring>  
#include <ctime>  
#include <deque>  
#include <exception>  
#include <fstream>  
#include <functional>  
#include <limits>  
#include <list>  
#include <map>  
#include <iomanip>  
#include <ios>  
#include <iosfwd>  
#include <iostream>  
#include <istream>  
#include <ostream>  
#include <queue>  
#include <set>  
#include <sstream>  
#include <stdexcept>  
#include <streambuf>  
#include <string>  
#include <utility>  
#include <vector>  
#include <cwchar>  
#include <cwctype>  
#include <stack>  
#include <limits.h> 
using namespace std;
#define MAXN 25

int i,T,n;
long long c;
long long f[MAXN][MAXN][2];
bool used[MAXN];

inline void dp()
{
        int i,j,k;
        f[1][1][0] = f[1][1][1] = 1;
        for (i = 2; i <= 20; i++)
        {
                for (j = 1; j <= i; j++)
                {
                        for (k = j; k < i; k++) f[i][j][0] += f[i - 1][k][1];
                        for (k = 1; k < j; k++) f[i][j][1] += f[i - 1][k][0]; 
                }
        }
}
inline void calc(int n,long long c)
{
        int i,j,rk,k,pre,opt;
        memset(used,false,sizeof(used));
        for (i = 1; i <= n; i++)
        {
                if (f[n][i][1] >= c) 
                {
                        pre = i;
                        opt = 1;
                        break;
                } else c -= f[n][i][1];
                if (f[n][i][0] >= c)
                {
                        pre = i;
                        opt = 0;
                        break;
                } else c -= f[n][i][0];
        }
        printf("%d ",pre);
        used[pre] = true;
        for (i = 2; i <= n; i++)
        {
                opt ^= 1;
                rk = 0;
                for (j = 1; j <= n; j++)
                {
                        if (used[j]) continue;
                        rk++;
                        if ((opt == 0 && j < pre) || (opt == 1 && j > pre))
                        {
                                if (f[n - i + 1][rk][opt] >= c)
                                {
                                        pre = j;
                                        break;
                                } else c -= f[n - i + 1][rk][opt];
                        }
                }
                printf("%d ",pre);
                used[pre] = true;
        }
        printf("
");
}

int main() 
{
        
        dp();
        scanf("%d",&T);
        while (T--)
        {
                scanf("%d%lld",&n,&c);    
                calc(n,c);
        }
        
        return 0;
    
}

 

以上是关于[POJ 1037] A decorative fence的主要内容,如果未能解决你的问题,请参考以下文章

编写带参数decorator

python 装饰器学习(decorator)

python--decorator装饰器

vijos 1037 背包+标记

Codeforces 1037F. Maximum Reduction

Python decorator装饰器