P3205 [HNOI2010] chorus [interval dp]

Title Description

In order to have a better performance results in the upcoming party, as the person in charge of AAA chorus A small chorus of people need to discharge a formation based on their height. Chorus assumed a total of N individuals, the i-th individual's height is Hi meters (1000 <= Hi <= 2000), and is known to any two people's height is different. Assuming A is finally discharged personal station formations in a row, in order to simplify the problem, a small A devised queuing follows: He let people press stand in any order with an initial formation, and then from left to right in the following the principle of each person in turn inserted into the final raft formation in the discharge of:

- the first person to empty directly into the current formation.

- for everyone starting from the second person, if he is taller than the person in front (H larger), then he will insert the current formation of the far right. If he is the man than the previous low (H small), then he inserts the current formation of the far left.

When N persons obtained after full insertion this formation formation finally exhausted.

For example, there are six initial formation into a personal station, and height were 1850,1900,1700,1650,1800 1750,

A small formation then get the final discharge of the following steps:

1850

  • 1850, 1900 because 1900> 1850
  • 1700, 1850, 1900 because 1700 <1900
  • 1650.1700, 1850, 1900 because 1650 <1700
  • 1650, 1700, 1850, 1900, 1800 because 1800> 1650
  • 1750, 1650, 1700,1850, 1900, 1800 because 1750 <1800

Thus, the formation is finally discharged 1750,1650,1700,1850, 1900,1800

A small heart has an ideal formation, he wanted to know how many you can get a good initial formation formation

Description / Tips

30% Data: n <= 100

100% Data: n <= 1000

Resolve

In fact, this question is recursive / record search

Observed title, easily summed up every time you take a person to join formation, he can only be added to the queue at the far left or the far right, to meet the nature of the interval dp.

Set \ (dp [0/1] [i ] [j] \) indicates the interval \ (i \ sim j \) the number of programs finally put the people in the most left / right.

According to the principle of addition, it is easy to write the state transition equation:
\ [DP [I] [J] [0] DP = [I +. 1] [J] [0] · [H_i <+ H_ {I}. 1] + DP [ i + 1] [j] [ 1] · [h_i <h_j] \\ dp [i] [j] [1] = dp [i] [j - 1] [0] · [h_j> h_i] + dp [ i] [j - 1] [
1] · [h_j> h_ {j-1}] \] I think this problem relatively magical thing (I WA several times), is initialized, the ghost know why only initialize a one-dimensional ( \ (0/1 \) that dimension), and no matter which dimension you initialize the answer is the same.

Reference Code

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#define mod 19650827
#define N 1010
using namespace std;
int dp[2][N][N],n,a[N];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;++i) scanf("%d",&a[i]),dp[0][i][i]=1;
    for(int len=2;len<=n;++len)
     for(int l=1;l<=n-len+1;++l){
        int r=l+len-1;
        // 0 left 1 right
        int t1=0,t2=0,t3=0,t4=0;
        if(a[l]<a[l+1]) t1=1;
        if(a[l]<a[r]) t2=1;
        if(a[r]>a[r-1]) t3=1;
        if(a[r]>a[l]) t4=1;
        dp[0][l][r]=(dp[0][l+1][r]*t1%mod+dp[1][l+1][r]*t2%mod)%mod;
        dp[1][l][r]=(dp[1][l][r-1]*t3%mod+dp[0][l][r-1]*t4%mod)%mod;
     }
    printf("%d\n",((dp[0][1][n]%mod+dp[1][1][n])%mod)%mod);
    return 0;
}

Guess you like

Origin www.cnblogs.com/DarkValkyrie/p/11295311.html