题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4489点击打开链接

The King’s Ups and Downs

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

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

解释起来有点复杂

首先 这是一个像波一样的 我们将高的称做波峰 矮的称做波谷

当新增加的一人插在队伍两边的时候 a[i]+=a[i-1]

当新增的人插在中间的时候 我们就对其中的一段进行排列组合

同时注意 选取的这两段的两端必须同为波峰或者波谷 而这种情况出现为当前这段总数的1/2

因此就可以知道 当前的a[i] 除了放在两边意外 还有内部所有排列组合可能出现的情况的递推 而这些就是计算新增加元素在内部的个数

注意排列组合其中一段为1时特判

#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include <limits>
#include <string>
#include <string.h>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;
long long int jc(int x)
{long long int sum=1;for(int i=2;i<=x;i++)sum*=i;return sum;
}
long long int c(int x,int y)
{return jc(x)/jc(y)/jc(x-y);
}
int main()
{long long int a[60];memset(a,0,sizeof(a));a[1]=1;a[2]=2;a[3]=4;for(int i=4;i<=20;i++){a[i]+=a[i-1];for(int j=1;j<i-1;j++){if(j==1||i-1-j==1)a[i]+=(a[j]*a[i-j-1]>>1)*c(i-1,j);elsea[i]+=(a[j]>>1)*(a[i-j-1]>>1)*c(i-1,j);}}int n;scanf("%d",&n);while(n--){int mid,m;scanf("%d%d",&m,&mid);printf("%d %lld\n",m,a[mid]);}}

HDU - 4489 The King’s Ups and Downs (排列组合+dp)相关推荐

  1. HDU 4489 The King’s Ups and Downs 组合DP

    The King's Ups and Downs HDU - 4489 给一个整数 nnn,求 {1,⋯,n}\{1,\cdots,n\}{1,⋯,n} 符合以下条件的的排列数: 设 a1,⋯,ana ...

  2. HDU 4489 The King’s Ups and Downs(组合DP)

    作者:Accagain 链接:点击打开链接 原题 The King's Ups and Downs Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...

  3. 6177 The King’s Ups and Downs(组合dp)

    欢迎访问https://blog.csdn.net/lxt_Lucia-- 宇宙第一小仙女\(^o^)/--萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗-- ---------- ...

  4. hdu 4489 The King’s Ups and Downs【递推】

    题目 http://acm.hdu.edu.cn/showproblem.php?pid=4489 题意 给一个n,求n个高矮不同的人排成一排使得高.矮依次排列的种数. 分析 我们不妨把n个人的身高设 ...

  5. hdu 4489 The King’s Ups and Downs

    看到n<=20我就想暴力打表找出所有情况了:但死在第15组,要花的时间太久了: 然后就想着推嘛:每次加入一个当前情况最大的数:合理的位置就是高低高低高:所以要找前面高低的情况n,后面低高的情况m ...

  6. hdu 4489 The King’s Ups and Downs 组合 递推

    题意:给定n个个数1~n.要求他们排成一列满足"波浪型".第一个大于第二个,第二大于第三个,(或者第一个小于第二个,第二个小于第三个..)也就是相对的高矮高矮...或矮高矮高..依 ...

  7. HDU 4489 The King's Ups and Downs

    题目链接 问的是n个不一样的数,大小交替,或者小大交替的种类数量. n个数,想象成[1,n]的自然数即可. 我们假设大小交替得到的长度为i的排列数为dp1[i],小大交替得到的长度为i的排列数为dp2 ...

  8. HDU - 4489 The King’s Ups and Downs(dp)

    题目链接 题目大意: 给一个n,求身高为1-n的n个人排成 高低高低高低  或 低高低高低高 这种波浪式的形状有多少种: 思路: 把第n个人插到前n-1个人的序列中,序列中共有n个空位,第n个人一定是 ...

  9. The King’s Ups and Downs (线性DP)

    The King's Ups and Downs (线性DP) [link](Problem - 4489 (dingbacode.com)) 题意 给你n个身高不同的人,问你这些人按照波浪形排列有多 ...

最新文章

  1. python提交表单无效_使用Django Form解决表单数据无法动态刷新的两种方法
  2. Fragment-FragmentMannager中的方法
  3. Leetcode: Reverse Linked List II
  4. 计算机学的语数英大概是那些,职高如果选电子商务专业那是不是要学语数英
  5. 《毅力–如何培养自律的习惯》读书笔记
  6. 能力=知识+技能+经验
  7. 关于Chrome出现Provisional headers are shown无法正常访问的解决方案(其他firefox,360, IE访问正常) (转)...
  8. 面试中几个基本的重要问题总结
  9. 从数据仓库系统对比看Hive发展前景
  10. linux内核镜像的分层,Docker 入门教程:镜像分层
  11. 不知不觉,写了一个编译器(一)
  12. 国内常见的日内CTA策略介绍以及实现
  13. 骇客基础知识:第3部分
  14. 请求的操作需要提升 windows7 route add命令 windows7添加路由
  15. csgo怎么一直连接服务器失败,CSGO提示连接到官方任意服务器失败怎么办?
  16. USACO-Fractions to Decimals
  17. 【个人记录|环境配置等】
  18. win10浏览器html乱码,win10系统浏览器字体乱码的解决方法
  19. css grid布局中的minmax()函数的使用
  20. 《数学建模》知识点总结

热门文章

  1. phpmywind 子菜单调用
  2. oracle查询语句 switch,ORACLE SQL语句中的“SWITCH语句”函数DECODE
  3. 2015年移动Web/HybridApp开发技能列表
  4. 【猿说VUE】Vue列表渲染
  5. android存储视频文件夹在哪,Android 中 视频存储路径的一个方案
  6. 如何以活动价在官网购买百度网盘会员
  7. hdf heg 批量拼接_python调用HEG工具批量处理MODIS数据
  8. 中国二维码--汉信码(中国主导的首个二维码码制国际标准「汉信码」ISO/IEC 20830:2021《信息技术 自动识别与数据采集技术 汉信码条码符号规范》)
  9. Latex爬过的坑(2)——I was expecting a ‘,‘ or a ‘}‘
  10. 关于思科无线网络无法连接解决