题目:

The King’s Ups and Downs

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1261    Accepted Submission(s): 897

Problem Description

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:

or perhaps:

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 arrange 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

Greater New York 2012

题意:

已知有N个士兵,求将他们排成高低高。。或者 低高低。。。

这种高低交错的一队有多少种排法。

分析: (转自:https://blog.csdn.net/wu_tongtong/article/details/77731671)
一道题不会做,我们的原则是什么:

打~表~找~规~律~

n=1 : 1 
n=2 : 2 
n=3 : 4 
n=4 : 10 
n=5 : 32 
n=6 : 122 
n=7 : 544 
n=8 : 2770 
n=9 : 15872 
n=10: 101042

但是并没有看出什么, 
我们还是需要化繁为简, 
如果现在我们有i个高度,已经排好了i-1个高度, 
现在需要放第i个高度(也就是最高的) 
因为是最高的,所以不管放在哪,ta都是山峰 
那ta的前面一定是 高低 
后面一定是 低高 
只有这样才能符合波浪形的限制

设f[i][0]表示长度为i且结尾是高低的序列数 
f[i][1]表示长度为i且开头是低高的序列数

假设我们现在要求长度为i的序列数, 
枚举第i个的插入位置j

ans[i]+=f[j][0]*f[i-j-1][1]*C(i-1,j)

C(i-1,j)是组合数(从i-1中取出j个)

那么现在的问题就变成了f[i][0/1]怎么求 
我们把一个序列抽象成01串,1表示山峰,0表示山谷

i个高度排好后无非两种情况 
开始为低高或开始为高低,那么排列的逆序也满足条件, 
也就是说结尾为高低的方法数和开始为低高的方法数相同 
而对于人数一定的情况,开始为低高的人数和开始为高低的人数相等

证明: 
当n为偶数时:假设波峰开始的序列为1010.那么把它倒置一下就变成了0101了 
也就是说每一个1打头的对应着一个0打头的

当n为奇数时:假设波峰开始的序列为10101. 
假设第一个数大于最后一个数,那我们把序列最后一个数放到最前序列就变成01010. 
如果第一个数小于最后一个数把第一个数放到最后就行了

所以每一个1打头的对应着一个0打头的

所以f[i][0]=f[i][1]=ans[i]/2;

这样就可以完成递推了

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
int p;
int n;
ll c[25][25];
ll f[25][2];
ll ans[25];
void init()
{c[1][0]=1;c[1][1]=1;int i,j,k;memset(ans,0,sizeof(ans));for(i=2;i<=20;i++){for(j=0;j<=20;j++){if(j==0){c[i][j]=1;}else {c[i][j]=c[i-1][j]+c[i-1][j-1];}}}
}
void find()
{f[1][0]=f[1][1]=1;f[0][0]=f[0][1]=1;int i,j,k,val;for(i=2;i<=20;i++){for(j=0;j<i;j++){ans[i]+=c[i-1][j]*f[j][0]*f[i-1-j][1];}f[i][0]=f[i][1]=ans[i]/2;}
}
int main()
{scanf("%d",&p);init();find();ans[1]=1;while(p--){int i,j,k,val;scanf("%d%d",&k,&val);printf("%d %lld\n",k,ans[val]);}return 0;
}

Contest1819 - 2019年我能变强组队训练赛第十一场相关推荐

  1. Contest1819 - 2019年我能变强组队训练赛第十一场(补题场)

    赛后总结 菜 问题 E: Faulhaber's Triangle 问题 E: Faulhaber's Triangle 单纯模拟题,模拟分数加法 代码 #include <bits/stdc+ ...

  2. 石油大 Contest1777 - 2019年第二阶段我要变强个人训练赛第九场 I 热狗树(树形dp)

    题目描述 "我是番茄酱!" "我是黄芥末酱!" "合在一起就是--美式热狗上加的,那个!" 热狗树上的每个节点都涂有番茄酱或者黄芥末酱中的一 ...

  3. 2019年第二阶段我要变强个人训练赛第十八场 扶桑号战列舰(线段树+递归)

    问题 N: 扶桑号战列舰 时间限制: 1 Sec  内存限制: 128 MB  Special Judge 题目描述 众所周知,一战过后,在世界列强建造超无畏级战列舰的竞争之中,旧日本海军根据&quo ...

  4. Contest1802 - 2019年第二阶段我要变强个人训练赛第十八场 问题 N: 扶桑号战列舰 线段树+贪心

    题目链接:http://icpc.upc.edu.cn/problem.php?cid=1802&pid=13 问题 N: 扶桑号战列舰 时间限制: 1 Sec  内存限制: 128 MB   ...

  5. 石油大 2019年第二阶段我要变强个人训练赛第十八场 Problem N 扶桑号战列舰(线段树+区间更新+区间查询)

    链接:http://icpc.upc.edu.cn/problem.php?cid=1803&pid=13 题意:给出一个n,接下来一行给出n个数.才开始所有数为0,每次操作可以选一个区间[l ...

  6. UPC2018组队训练赛第六场

    题目来自UKIEPC2017 A题:Alien Sunset 有n个星球,输入每个星球一天的时间,日出和日落的时间.从日落到日出(包括日出.日落)是黑夜.其他的为白天.问在前1825天里能不能有一个时 ...

  7. 石油大--2020年秋季组队训练赛第十三场----B、Bouldering(最短路)

    题面: 题意: 给定一个 h∗wh*wh∗w 点阵,其中某一些点是可以走的. 这些点都有一个权值,表示如果经过当前点,则会花费的力气. 给定一个 rrr,你只能从当前点到达与你欧几里得距离不超过 rr ...

  8. 石油大--2020年秋季组队训练赛第十三场---- C、Colourful Chameleons(思维)

    题面: 题意: 有 nnn 种颜色的变色龙,其中第 iii 种颜色的变色龙有 ai,(ai>=n−1)a_i ,(a_i>=n-1)ai​,(ai​>=n−1) 只. 我每次可以选择 ...

  9. 2018年第四阶段组队训练赛第七场

    A: Secret of Chocolate Poles 题目描述 Wendy, the master of a chocolate shop, is thinking of displaying p ...

最新文章

  1. AOC的显示器真的很烂
  2. PHP求体重成绩函数,PHP数组
  3. Java NIO编写Socket服务器的一个例子
  4. python压缩文件
  5. 建设数据中台之前,建议先看这份企业数据能力测评 | 大咖说中台
  6. js统计页面访问次数
  7. Ring3加载驱动源码
  8. 皮尔森相关系数与方差膨胀因子介绍及关系 附python代码
  9. php免费人机验证,Antiboter是一个界面漂亮且方便使用的PHP后台的图片人机验证
  10. 75道逻辑思维题及答案
  11. Xinetd服务的安装与配置
  12. clickhouse建表异常 DB::Exception: No macro ‘shard‘ in config
  13. Halide学习笔记----Halide tutorial源码阅读2
  14. 配置NetBackup 7 for oracle 10g rac
  15. t460p加固态硬盘 thinkpad_不仅是硬件升级!ThinkPad T460p评测
  16. Qt之QWidget设置窗口背景图片的几种方法
  17. 影之刃服务器维护,《影之刃3》骨灰级玩家给大家带来一条龙攻略服务
  18. 博客大巴发布模块,如何使用?
  19. Python创建递增数列、递减数列、等差数列
  20. Android 之 TranslateAnimation类:位移动画类(转载)

热门文章

  1. 智能拐杖(检测障碍物)
  2. ASSA脚本指令中文详细说明
  3. brew cask install 和 brew install 的区别
  4. 缘起---回头再说 坚强2002
  5. 科目余额表-调整格式
  6. 【adoo】Van Emde Boas trees
  7. 数学最高奖菲尔兹奖得主 Laurent Lafforgue 官宣加入华为!
  8. 2023广州大学计算机考研信息汇总
  9. 渐进地了解渐进式框架Vue
  10. WPF随笔(六)--查看网络图片