10128-queue
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_ problem&problem=1069
Now that you have the best substructure, consider it from a DP perspective.
There are three different dimensions: Total number N, formerly seen p, the person seen from the later R.
Assuming that the queue is now made by the i-1 individual, it doesn't matter who is backward to the queue, suppose that the shortest person is the last to enter the queue, then its position will have three kinds of situation, the first situation is to stand at the head of the team, to increase the number of people seen in front, the second situation is to stand at the end of the team, To increase the number of people to see in the back, the third situation is to stand in the middle of the line, a total of i-2 positions can stand, but will not increase the number of visible. So that you can get:
dp[i][j][k]=dp[i-1][j-1][k]+dp[i][j][k-1]+ (i-2) *dp[i-1][j][k]
Complete code:
/*0.019s*/
#include <cstdio>
#include <cstring>
long long f[20][20][20];
void prepare ()///combination too troublesome, decisively recursive
{
int i, j, K;
F[1][1][1] = 1;
for (i = 2; I <=. ++i) for
(j = 1; J <= i; ++j) for
(k = 1; k <= i; ++k)
f[i][j][k] = f[i-1][j -1][k] + f[i-1][j][k-1] + (i-2) * f[i-1][j][k];
}
int main ()
{
prepare ();
int T, N, P, Q;
scanf ("%d", &t);
while (t--)
{
scanf ("%d%d%d", &n, &p, &q);
printf ("%lld\n", F[n][p][q]);
return 0;
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/