UVa 10128 Queue (DP)

Source: Internet
Author: User
Tags prepare time limit

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.