UVA10128 Queue

来源:互联网 发布:中国好新歌声网络直播 编辑:程序博客网 时间:2024/05/16 10:08
There is a queue with N people. Every person has a different heigth. We can see P people, when we
are looking from the beginning, and R people, when we are looking from the end. Its because they
are having different height and they are covering each other. How many different permutations of our
queue has such a interesting feature?
Input
The input consists of T test cases. The number of them (1 ≤ T ≤ 10000) is given on the first line of
the input file.
Each test case begins with a line containing a single integer number N that indicates the number
of people in a queue (1 ≤ N ≤ 13). Then follows line containing two integers. The first integer
corresponds to the number of people, that we can see looking from the beginning. The second integer
corresponds to the number of people, that we can see looking from the end.
Output
For every test case your program has to determine one integer. Print how many permutations of N
people we can see exactly P people from the beginning, and R people, when we are looking from the
end.
Sample Input
3
10 4 4
11 3 1
3 1 2
Sample Output
90720
1026576

1


这道题是从大神的博客里面看到的解法,就可以想象自己正在向队列中插入人数,有三种方式,一种是放在最左边,一种是放在最右边,还有一种是

放在中间,也就是都看不到的地方,我认为这种思想就是像递归,用一个三维的数组,i表示,之前已经放好的人数,j表示从前面可以看到的人数,而

k表示从后面可以看到的人数,那么假设前面一种的情况是已经确定了的,那么此时的情况也就建立在之前情况下解决,而不用完全去想清楚之前的那种

情况是什么,这就是我的理解


#include<iostream>#include<cstring>using namespace std;int qun[15][15][15];int main(){    int times;    cin>>times;    while(times--)    {        memset(qun,0,sizeof(qun));        int n,p,r;        cin>>n>>p>>r;        qun[1][1][1] = 1;        for(int i = 2;i<=n;i++)            for(int j = 1;j<=i;j++)            for(int k = 1;k<=i;k++)            qun[i][j][k] = qun[i-1][j-1][k] + qun[i-1][j][k-1] + (i-2)*qun[i-1][j][k];        cout<<qun[n][p][r]<<endl;    }}


0 0