This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* 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 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[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]);
}
memset(dp[(i+1)&1], 0, sizeof(dp[(i+1)&1]));
}
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |