• UVALive 6177 The King's Ups and Downs


    The King's Ups and Downs

    Time Limit: 3000ms
    Memory Limit: 131072KB
    This problem will be judged on UVALive. Original ID: 6177
    64-bit integer IO format: %lld      Java class name: Main
     

    The king has guards of all different heights. Rather than line them up in increasing or decreasing height order, he wants to line them up so each guard is either shorter than the guards next to him or taller than the guards next to him (so the heights go up and down along the line). For example, seven guards of heights 160, 162, 164, 166, 168, 170 and 172 cm. could be arranged as:

    epsfbox{p6177a.eps}


    or perhaps:

    epsfbox{p6177b.eps}

    The king wants to know how many guards he needs so he can have a different up and down order at each changing of the guard for rest of his reign. To be able to do this, he needs to know for a given number of guards, n, how many different up and down orders there are:

    For example, if there are four guards: 1, 2, 3, 4 can be arranged as:

    1324, 2143, 3142, 2314, 3412, 4231, 4132, 2413, 3241, 1423

    For this problem, you will write a program that takes as input a positive integer n, the number of guards and returns the number of up and down orders for n guards of differing heights.

    Input
    The first line of input contains a single integer P, (1 <= P <= 1000), which is the number of data sets that follow. Each data set consists of single line of input containing two integers. The first integer, D is the data set number. The second integer, n (1 <= n <= 20), is the number of guards of differing heights.
     

     

    Output
    For each data set there is one line of output. It contains the data set number (D) followed by a single space, followed by the number of up and down orders for the n guards.
     


    Sample Input
    4
    1 1
    2 3
    3 4
    4 20
     


    Sample Output
    1 1
    2 4
    3 10
    4 740742376475050

    Source

     
    解题:吗各级,动态规划
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const int maxn = 23;
     5 LL dp[maxn][2],c[maxn][maxn];
     6 void init(){
     7     for(int i = 0; i <= 20; ++i){
     8         c[i][0] = c[i][i] = 1;
     9         for(int j = 1; j < i; ++j)
    10             c[i][j] = c[i-1][j-1] + c[i-1][j];
    11     }
    12     dp[1][0] = dp[1][1] = dp[0][0] = dp[0][1] = 1;
    13     for(int i = 2; i <= 20; ++i){
    14         LL tmp = 0;
    15         for(int j = 0; j < i; ++j)
    16             tmp += dp[j][0]*dp[i - j - 1][1]*c[i-1][j];
    17         dp[i][0] = dp[i][1] = (tmp>>1);
    18     }
    19 }
    20 int main(){
    21     init();
    22     int kase,cs,n;
    23     scanf("%d",&kase);
    24     while(kase--){
    25         scanf("%d%d",&cs,&n);
    26         printf("%d %lld
    ",cs,n == 1?1:dp[n][0]<<1);
    27     }
    28     return 0;
    29 }
    View Code
  • 相关阅读:
    【Git】分支管理
    【Java】jprofiler工具初上手
    【中间件】JMS
    【Idea】编译问题Intellij Error: Internal caches are corrupted or have outdated format
    【测试】测试用例选择
    【DevOps】
    【Unix】z/OS系统
    【数据库】表空间释放
    【数据库pojo类】Oracle数据库中各数据类型在Hibernate框架中的映射
    基于闭包实现的显示与隐藏
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4788877.html
Copyright © 2020-2023  润新知