HDU 5151Sit sit sit

Source: Internet
Author: User

An interval DP problem, a bestcoder of the B question, thought for a long time did not think out.

Title Description: There are a total of n chairs, n students to sit in turn, at the same time meet 3 conditions can not sit down: 1, the chair is not the most left, not the right, 2, the chair is sitting around, 3, about the chair different colors
Ask the last n people can sit down, how many different situations.

The dp[i][j] indicates the number of rows [i,j] of the interval, (to see the other person not to understand the resolution, people are in order to determine the position of the seat, that is, in the interval [i,j] the sequence of seats. When [I,j]->[1,n], the type of DP is not the number of n individuals in order to decide the position of the type of sitting )

Transfer equation: dp[i][j] = SUM (dp[i][k-1]*dp[k+1][j]* c[j-i][i-k]) where v[k-1]==v[k+1], the last one is guaranteed to be the most lawful. The reason for multiplying the number of combinations is that in the j-i individuals the person who decides to sit in the previous interval (they are ordered after the election).

Code June:

#include <iostream> #include <cstdio> #include <cstring> #include <cmath>const int N = 100+10; const INT mod = 1e9+7;typedef long long ll;ll dp[n][n];int a[n];ll c[n][n];void init () {for (int i=0;i<=100;i++) C[i]    [0] = 1; for (int i=1;i<=100;i++) for (int j=1;j<=i;j++) c[i][j] = (C[i-1][j-1] + c[i-1][j])%mod;}    ll DFS (int i,int j) {if (i>j) return 0;    if (i==j) return dp[i][j]=1;    if (dp[i][j]!=-1) return dp[i][j];    ll ans = 0;    Ans = (Dfs (i+1,j) + DFS (i,j-1))%mod;        for (int k=i+1;k<=j-1;k++) {if (a[k-1]!=a[k+1]) continue;    Ans = (ans + dfs (i,k-1) *dfs (k+1,j)%mod*c[j-i][k-i]%mod)%mod; } return Dp[i][j] = ans;}    int main () {int n;    Init ();        while (scanf ("%d", &n)!=eof) {for (int i=1;i<=n;i++) scanf ("%d", &a[i]);        Memset (Dp,-1,sizeof (DP));        ll ans = DFS (1,n);    printf ("%lld\n", ans); } return 0;}



HDU 5151Sit sit sit

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.