forked from THUNDERANKUSH/HACKERRANK-CODES
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSherlock's Array Merging Algorithm.cpp
More file actions
110 lines (90 loc) · 1.81 KB
/
Sherlock's Array Merging Algorithm.cpp
File metadata and controls
110 lines (90 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#pragma comment(linker, "/STACK:1000000000")
#include <cstdio>
#include <iostream>
#include <ctime>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <set>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <bitset>
#include <fstream>
#include <deque>
#include <stack>
#include <climits>
#include <string>
#include <queue>
#include <memory.h>
#include <map>
#include <unordered_map>
#define ll long long
#define ld double
#define pii pair <ll, ll>
#define mp make_pair
using namespace std;
const int maxn = 1500;
const int mod = (int)1e9 + 7;
ll dp[maxn][maxn];
int v[maxn];
int n;
int lnk[maxn];
ll fac[maxn];
ll C[maxn][maxn];
ll getCC(int a, int b) {
if (C[a][b] != -1) {
return C[a][b];
}
if (a == b || b == 0) {
C[a][b] = 1;
} else {
C[a][b] = (getCC(a - 1, b) + getCC(a - 1, b - 1)) % mod;
}
return C[a][b];
}
ll go(int pos, int cnt) {
if (dp[pos][cnt] != -1) {
return dp[pos][cnt];
}
if (cnt == 0) {
return dp[pos][cnt] = 0;
}
if (pos == n) {
return dp[pos][cnt] = 1;
}
ll ans = 0;
for (int k = min(cnt, n - pos); k >= 0; k--) {
if (lnk[pos] >= pos + k - 1) {
ans += (pos == 0 ? 1 : getCC(cnt, k) * fac[k] % mod) * go(pos + k, k);
ans %= mod;
}
}
return dp[pos][cnt] = ans;
}
int main() {
memset(C, -1, sizeof C);
memset(dp, -1, sizeof dp);
fac[0] = 1;
for (int i = 1; i < maxn; i++) {
fac[i] = fac[i - 1] * i % mod;
}
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%d", &v[i]);
//v[i] = i + 1;
}
for (int i = n - 1; i >= 0; i--) {
if (i == n - 1) {
lnk[i] = i;
} else if (v[i] < v[i + 1]) {
lnk[i] = lnk[i + 1];
} else {
lnk[i] = i;
}
}
cout << go(0, n) << endl;
return 0;
}