题目描述

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

输入

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.

输出

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.

样例输入

4
1 1
2 3
3 4
4 20

样例输出

1 1
2 4
3 10
4 740742376475050

题目链接:点击查看


题目大意:给定n个士兵,身高分别为1~n,各不相同,问符合条件的排列有多少种,符合条件的排列为:

  1. 奇数位的士兵全部高于偶数位的士兵
  2. 奇数位的士兵全部低于偶数位的士兵

满足上述两个条件的任意一条即视为符合条件

题目分析:这个题目和之前做过的一道数位dp很像,叫Mountain number,也是让求一定区间范围内满足条件的数字有多少种,那么我们可以借助这个思想,利用记忆化搜索来做,因为如果只是单纯的暴力递归,那么需要遍历n的阶乘的时间复杂度,20的阶乘就到了1e19的程度了,所以暴力枚举肯定会超时,所以我们选用记忆化搜索,举个很简单的例子,如果n为4,那么其中的两种情况为1234和1243,显而易见,前两位的12被枚举了两次,如果n变大,那么重复递归的时间复杂度呈指数级别上升,多做了很多无用的操作,所以我们采取记忆化,类似于数位dp的记忆化,开一个数组dp[pos][pre][state],pos代表的是位数,pre代表的是前面的一个数,state代表的是该上升还是该下降,这样就能将前两位为12的次数记下来,然后在和后面两位为34和43的相加,这样需要枚举的次数大大降低,就可以再规定范围内实现这个题目了,上代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<sstream>
using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e5+100;int n;LL dp[25][25][2];//pos,prebool vis[25];LL ans=0;LL dfs(int pos,int pre,bool state)//state:0表示该上升,1表示该下降
{if(pos==0)return 1;if(dp[pos][pre][state]!=-1)return dp[pos][pre][state];LL ans=0;for(int i=1;i<=n;i++){if(vis[i])continue;if(state&&pre>i){vis[i]=true;ans+=dfs(pos-1,i,!state);vis[i]=false;}else if(!state&&pre<i){vis[i]=true;ans+=dfs(pos-1,i,!state);vis[i]=false;}}return dp[pos][pre][state]=ans;
}int main()
{
//  freopen("input.txt","r",stdin)int w;cin>>w;while(w--){int num;memset(dp,-1,sizeof(dp));cin>>num>>n;if(n==1)//1记得要特判{printf("%d 1\n",num);continue;}memset(vis,false,sizeof(vis));cout<<num<<' '<<dfs(n,-1,0)+dfs(n,25,1)<<endl;}return 0;
}

中石油训练赛 - The King’s Ups and Downs(记忆化搜索)相关推荐

  1. 中石油训练赛 - High Load Database(二分+记忆化)

    题目链接:点击查看 题目大意:给出一个长度为 n 的数列,再给出 m 次询问,每次询问给出一个阈值 x ,问最少将数列分割成多少段,可以使得每一段的总和都不超过 x,无解的话输出 Impossible ...

  2. 中石油训练赛 - Trading Cards(最大权闭合子图)

    题目大意:给出 n 个卡片,可以自由买卖,且价格都是相同的,再给出 m 个集合,如果已经得到了其中一个集合中的卡片,那么可以获得该集合的收益,问如何操作可以使得收益最大化 题目分析:最大权闭合子图的模 ...

  3. 中石油训练赛 - Watch Later(状压dp)

    题目链接:点击查看 题目大意: 给出一个长度为 n 的字符串,字符串中共有 k 种不同的字符,现在问删除掉所有字符的最小操作数,对于每种字符需要确定一个先后顺序,每次需要删除掉当前所有的这种字符才能去 ...

  4. 中石油训练赛 - Swapping Places(字典序最小的拓扑排序)

    题目链接:点击查看 题目大意:给出 s 个字符串表示种类,再给出 m 个朋友关系,表示两个种类的动物是朋友,现在给出一个长度为 n 的种类排列,规定相邻两个是朋友的种类的动物可以交换位置,问如何操作, ...

  5. 中石油训练赛 - Gone Fishing(固定大小的圆可以覆盖最多的点)

    题目大意:在二维平面中给出 n 个点,再给出一个固定大小的圆,问如何放置这个圆可以使其覆盖最多的点 题目分析:首先不难想到一种 n^3 的做法,就是两层循环去枚举两个点,因为两个不同的点就可以确定下来 ...

  6. 中石油训练赛 - Russian Dolls on the Christmas Tree(树上启发式合并/主席树)

    题目链接:点击查看 题目大意:给出一棵 n 个节点的树,以点 1 为根,现在对于每个节点作为根的子树求解:子树中有多少个编号不相交的连续子段,如:1 2 4 5 7,共有三个连续的段,分别为 [ 1 ...

  7. 中石油训练赛 - Check List(线段树维护偏序问题)

    题目大意:给出 n 个点,需要计算出满足下列条件的三元对 ( i , j , k ) 的数量: x[ i ] < x[ j ] < x[ k ] y[ k ] > y[ i ] &g ...

  8. 中石油训练赛 - Bad Treap(数学)

    题目链接:点击查看 题目大意:给出笛卡尔树的定义,现在要求给出 n 个点对 ( x , sin( x ) ),使得笛卡尔树的高度尽可能大 题目分析:如果想让笛卡尔树的高度尽可能大,令其退化为一条链即可 ...

  9. 中石油训练赛 - Plan B(点双缩点+树形dp)

    题目大意:给出一张 n 个点 m 条边的无向连通图,现在有某些点被标记了,问能否通过删除某个未被标记的点,使得删除该点后的数个互不相交的连通块中,至少存在一个联通块中不含有被标记的点 题目分析:首先不 ...

最新文章

  1. Python零基础自学会有哪些弊端
  2. SQL函数类的操作,增加,查询
  3. oracle两列同时去重_Oracle表中重复数据去重的方法实例详解
  4. 图像转换为二维数组存入DSP6748
  5. 3D Button Visual Editor
  6. C语言找出4个最大和4个最小数,济南大学C语言程序设计教案:C语言实验课程第四课.doc...
  7. Conda solving environment一晚上还不能完成有解吗?
  8. Mosquito的优化——其他优化(九)
  9. 0x00000000指令引用的内存不能为written_JVM03——对象实例化,内存布局,访问定位...
  10. Android五天乐(第三天)ListFragment与ViewPager
  11. 【pytorch】RuntimeError: 1only batches of spatial targets supported (3D tensors) but got targets of si
  12. java war 反编译_war反编译成java项目
  13. 基于VB的Picture绘图
  14. jquery中json数组转成对象的方法
  15. 使用 MVVMLight 消息通知
  16. UE4_C++中声明代理传入数组参数
  17. 神经质的实质与治疗--神经质的分类
  18. sqoop 工具的使用
  19. 股票、债券和期货对比
  20. Oracle修改用户密码引发的问题

热门文章

  1. RabbitMQ快速入门--简单队列模型
  2. 使用Docker-容器命令案例1
  3. Spring如何将事件分配给专门的监听器?
  4. 定位Bean 扫描路径
  5. Nacos安装和服务注册
  6. 悔不当初:回顾进化之路
  7. request的简介和运行环境
  8. 分布式文件系统研究-搭建图片服务虚拟主机
  9. jwt:token的解析
  10. 数据库-优化-数据库结构的优化-数据类型