10
7
2015
1

CF289 Div2 F. Progress Monitoring

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
 

   看到题目的第一眼就感到十分熟悉。。。后来才发现是半年前心血来潮(一时脑残)打CF现场赛时的题目。。。当时果真的太弱了。。这种题目都不会做。。。

   其实是很简单的记忆化搜索。。枚举子树的大小考虑一下限制什么的乱搞(口胡)一下就好了。。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int mod = 1e9+7;
int a[510],n,vis[510][510],f[510][510];
int read(){
	int x = 0,f = 1,c = getchar();
	while (c<'0' || c>'9'){if (c=='-') f = -1;c = getchar();}
	while (c>='0' && c<='9') x = x*10+c-'0',c = getchar();
	return x*f;
}
int dfs(int l,int r){
	if (l>r) return f[l][r] = 1;
	if (f[l][r]) return f[l][r];
	int res = dfs(l+1,r),pos = r;
	for (int i=l;i<r;i++)
		if (a[l]<a[i+1]) (res+=1ll*dfs(l+1,i)*dfs(i+1,r)%mod)%=mod;
	return f[l][r] = res%mod;
}
int main(){
//	freopen("tree.in","r",stdin);
//	freopen("tree.out","w",stdout);
	n = read();
	for (int i=1;i<=n;i++) a[i] = read();
	printf("%d",dfs(2,n));
}
Category: codeforces | Tags: | Read Count: 427
Avatar_small
cleaning services 说:
2022年5月23日 20:33

If it is been a bit since the house has also been professionally wiped clean or if it is never also been professionally cleaned a Deep Cleaning is things to get you started off on the suitable foot. We escape our finest and the majority precise tools for our deep cleanings. We guarantee that dust bunny is taken out, every space and cranny is usually wiped fresh and the many hard to realize places pick up the time period and care needed. Your home will likely be as clean in any other case cleaner when you do you migrated in!


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com