【第22期】观点:IT 行业加班,到底有没有价值?

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

原创 2015年07月08日 23:11:51
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) (A, B, C, E)

A:水题,根据题目预处理一下输出即可 B:先把最大和最小找出来,可以让最小全是1,然后最大比最小多出的部分就放1,2,3,4,5...所以如果MAX - MIN > k就是NO,不然就根据这个构造出...

Codeforces Round #116 (Div. 2, ACM-ICPC Rules) D - Name

这题细节真多!!!! #include&lt;iostream&gt; #include&lt;vector&gt; #include&lt;algorithm&gt; #include&lt;cstdio&gt; #include&lt;queue&gt; #include&lt;stack&gt; #include&lt;string&gt; #include&lt;map&gt; #include&lt;set&gt

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 #116 (Div. 2, ACM-ICPC Rules) E - Cubes

#include&lt;iostream&gt; #include&lt;vector&gt; #include&lt;algorithm&gt; #include&lt;cstdio&gt; #include&lt;queue&gt; #include&lt;stack&gt; #include&lt;string&gt; #include&lt;map&gt; #include&lt;set&gt; #include&lt;cmath&gt; #include&lt;cassert&gt; #include&

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

A. Maximum in Table time limit per test 2 seconds memory limit per test 256 megabytes ...

Codeforces Round #116 (Div. 2, ACM-ICPC Rules) C - Letter

#include&lt;iostream&gt; #include&lt;vector&gt; #include&lt;algorithm&gt; #include&lt;cstdio&gt; #include&lt;queue&gt; #include&lt;stack&gt; #include&lt;string&gt; #include&lt;map&gt; #include&lt;set&gt; #include&lt;cmath&gt; #include&lt;cassert&gt; #include&lt;cstrin

Codeforces Round #289 (Div. 2, ACM ICPC Rules) -- A. Maximum in Table

A. Maximum in Table time limit per test 2 seconds memory limit per test 256 megabytes ...

Codeforces Round #289 (abce)

A #include&lt;iostream&gt; #include&lt;cstring&gt; #include&lt;cstdio&gt; using namespace std; long long num[20][20]; int main(){ long long i,j,k,a,b,c; while(cin&gt;&gt;a){

Codeforces Round #153 (Div. 2)

点击打开链接cf #153 A 思路:暴力 分析: 1 题目给定n个数 n&lt;=100,要求一个区间内做^能

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

贪心,找比前一位大并且各位数字和为a[i]的最小的数。。。 #include #include #include #include #include #include #include ...
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)