# Codeforces Round #289 (Div. 2, ACM ICPC Rules) F. Progress Monitoring 区间dp

F. Progress Monitoring
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Programming teacher Dmitry Olegovich is going to propose the following task for one of his tests for students:

You are given a tree T with n vertices, specified by its adjacency matrix a[1... n, 1... n]. What is the output of the following pseudocode?

used[1 ... n] = {0, ..., 0};

procedure dfs(v):
print v;
used[v] = 1;
for i = 1, 2, ..., n:
if (a[v][i] == 1 and used[i] == 0):
dfs(i);

dfs(1);


In order to simplify the test results checking procedure, Dmitry Olegovich decided to create a tree T such that the result is his favorite sequence b. On the other hand, Dmitry Olegovich doesn't want to provide students with same trees as input, otherwise they might cheat. That's why Dmitry Olegovich is trying to find out the number of different trees T such that the result of running the above pseudocode with T as input is exactly the sequence b. Can you help him?

Two trees with n vertices are called different if their adjacency matrices a1 and a2 are different, i. e. there exists a pair (i, j), such that1 ≤ i, j ≤ n and a1[i][j] ≠ a2[i][j].

Input

The first line contains the positive integer n (1 ≤ n ≤ 500) — the length of sequence b.

The second line contains n positive integers b1, b2, ..., bn (1 ≤ bi ≤ n). It is guaranteed that b is a permutation, or in other words, each of the numbers 1, 2, ..., n appears exactly once in the sequence b. Also it is guaranteed that b1 = 1.

Output

Output the number of trees satisfying the conditions above modulo 109 + 7.

Sample test(s)
input
3
1 2 3

output
2

input
3
1 3 2

output
1
题意，给出一个序列，要求所能找出的对应树的个数，要求这个树深搜出的序列刚好是给出的这个序列！
用区间dp做，因为每个序列按某种规则可以切成两段，两段各对应的树，这样就可以把大问题化成小问题了！
具体公式可以推出 dp[s,e] = sum(dp[s+1,i] * dp[i,e]  条件(i==e || p[k+1] > p[s+1] ));
dp[s,e]表示，以s为根，从s - e的序列对应的树的个数，s+1 必须成为s 的 子树的一个根，又因为，p[k+1] > p[s+1],所以i+1,可以成为一个树的根，i+1
的父结点是s,也就是k+1,是s+1,的兄弟结点。但为什么这里要用dp[i][e]呢？因为，k+1的父结点是s,但s不能直接写，所以用i代替占一个位，结果的个数是不
变的，我们只要知道，i+1是一个子结点就可以了。当i == e时也要跳成dp[s+1][e],因为比如 1 2 3 4 这个序列dp[1,4]自然包括 dp[2,4]。
最后，这里要用long long，防溢出！
#define N 505
#define M 100005
#define maxn 205
#define MOD 1000000007
int n,pri[N];
ll dp[N][N];
ll DFS(int s,int e){
if(dp[s][e] >=0) return dp[s][e];
if(s == e) return dp[s][e] = 1;
dp[s][e] = 0;
for(int i=s+1;i<=e;i++){
if(i == e || pri[i + 1] > pri[s+1])
dp[s][e] = (dp[s][e] + DFS(s+1,i) * DFS(i,e)) % MOD;
}
return dp[s][e];
}
int main()
{
while(S(n)!=EOF)
{
FI(n)
S(pri[i]);
memset(dp,-1,sizeof(dp));
cout<<DFS(0,n-1)<<endl;
}
return 0;
}


• 本文已收录于以下专栏：

## Codeforces Round #289 (Div. 2, ACM ICPC Rules)

• wcr1996
• 2015年02月01日 00:19
• 1179

## Codeforces Round #289 (Div. 2, ACM ICPC Rules) B. Painting Pebbles

B. Painting Pebbles time limit per test 1 second memory limit per test 256 megabytes inpu...

## Codeforces Round #289 (Div. 2, ACM ICPC Rules)C. Sums of Digits

C. Sums of Digits time limit per test 2 seconds memory limit per test 256 megabytes input...

## Codeforces Round #289 (Div. 2, ACM ICPC Rules)E. Pretty Song

E. Pretty Song time limit per test 1 second memory limit per test 256 megabytes input st...

## Codeforces Round #289 (Div. 2, ACM ICPC Rules) -- B. Painting Pebbles （贪心）

B. Painting Pebbles time limit per test 1 second memory limit per test 256 megabytes ...

## Codeforces Round #145 (Div. 2, ACM-ICPC Rules)

• loinus
• 2012年10月17日 10:39
• 381

## Codeforces Round #116 (Div. 2, ACM-ICPC Rules) E - Cubes

#include #include #include #include #include #include #include #include #include #include #include #...
• zz_1215
• 2012年04月22日 17:52
• 448

## Codeforces Round #116 (Div. 2, ACM-ICPC Rules)---E. Cubes

Let’s imagine that you’re playing the following simple computer game. The screen displays n lined-up...

举报原因： 您举报文章：Codeforces Round #289 (Div. 2, ACM ICPC Rules) F. Progress Monitoring 区间dp 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)