/**
* author: Haunted_Cpp
**/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
template<typename T> ostream &operator << (ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename T, size_t size> ostream &operator << (ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream &operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); }
#ifdef LOCAL
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#else
#define debug(...) 47
#endif
const int N = 1e4 + 5;
const int MOD = 1e6 + 7;
int dp[2][N][2];
int add(int a, int b) {
int res = a + b;
if (res >= MOD) res -= MOD;
return res;
}
int mult(int a, int b) {
long long res = 1LL * a * b;
if (res >= MOD) res %= MOD;
return res;
}
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++) cin >> arr[i];
dp[0][1][0] = 1;
for (int i = 1; i < n; i++) {
for (int j = 1; j <= i + 1; j++) {
// Reach a new maximum
if(j < arr[i]) {
dp[i&1][j][1] = add(dp[i&1][j][1], dp[(i-1)&1][j-1][0]);
dp[i&1][j][1] = add(dp[i&1][j][1], dp[(i-1)&1][j-1][1]);
} else if (j == arr[i]) {
dp[i&1][j][0] = add(dp[i&1][j][0], dp[(i-1)&1][j-1][0]);
dp[i&1][j][1] = add(dp[i&1][j][1], dp[(i-1)&1][j-1][1]);
} else {
dp[i&1][j][1] = add(dp[i&1][j][1], dp[(i-1)&1][j-1][1]);
}
// Just use some old value, keep maximum
dp[i&1][j][1] = add(dp[i&1][j][1], mult(dp[(i-1)&1][j][1], j));
dp[i&1][j][1] = add(dp[i&1][j][1], mult(dp[(i-1)&1][j][0], min(j, arr[i] - 1)));
if (j >= arr[i]) dp[i&1][j][0] = add(dp[i&1][j][0], dp[(i-1)&1][j][0]);
}
for (int j = 1; j <= n; j++) dp[(i + 1) & 1][j][0] = dp[(i + 1) & 1][j][1] = 0;
}
int res = 0;
for (int i = 1; i <= n; i++) {
res = add(res, dp[(n - 1) & 1][i][0]);
res = add(res, dp[(n - 1) & 1][i][1]);
}
cout << res << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
382 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
512 KB |
Output is correct |
2 |
Correct |
113 ms |
632 KB |
Output is correct |
3 |
Correct |
96 ms |
516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
401 ms |
616 KB |
Output is correct |
2 |
Correct |
458 ms |
512 KB |
Output is correct |
3 |
Correct |
467 ms |
628 KB |
Output is correct |