/**
* 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[N][2][2];
int arr[N];
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;
for (int i = 0; i < n; i++) cin >> arr[i];
dp[1][0][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[j][i&1][1] = add(dp[j][i&1][1], dp[j-1][(i-1)&1][0]);
dp[j][i&1][1] = add(dp[j][i&1][1], dp[j-1][(i-1)&1][1]);
} else if (j == arr[i]) {
dp[j][i&1][0] = add(dp[j][i&1][0], dp[j-1][(i-1)&1][0]);
dp[j][i&1][1] = add(dp[j][i&1][1], dp[j-1][(i-1)&1][1]);
} else {
dp[j][i&1][1] = add(dp[j][i&1][1], dp[j-1][(i-1)&1][1]);
}
// Just use some old value, keep maximum
dp[j][i&1][1] = add(dp[j][i&1][1], mult(dp[j][(i-1)&1][1], j));
dp[j][i&1][1] = add(dp[j][i&1][1], mult(dp[j][(i-1)&1][0], min(j, arr[i] - 1)));
if (j >= arr[i]) dp[j][i&1][0] = add(dp[j][i&1][0], dp[j][(i-1)&1][0]);
}
for (int j = 1; j <= i + 1; j++) {
memset(dp[j][(i+1)&1], 0, sizeof(dp[j][(i+1)&1]));
}
}
int res = 0;
for (int i = 1; i <= n; i++) {
res = add(res, dp[i][(n - 1) & 1][0]);
res = add(res, dp[i][(n - 1) & 1][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 |
1 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 |
1 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 |
0 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 |
4 ms |
416 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
373 ms |
652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
384 KB |
Output is correct |
2 |
Correct |
99 ms |
632 KB |
Output is correct |
3 |
Correct |
94 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
392 ms |
516 KB |
Output is correct |
2 |
Correct |
379 ms |
496 KB |
Output is correct |
3 |
Correct |
372 ms |
508 KB |
Output is correct |