This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e4 + 10;
const ll mod = 1e6 + 7;
ll n, ans, a[N], dp[2][N];
int main(){
cin >> n;
ll mx = 0;
vector<pair<int, int>> vec;
vector<int> vals;
for (ll i=1; i<=n; i++){
cin >> a[i];
if (i < n)
vec.push_back({i + 1, mx + 1});
mx = max(mx, a[i]);
}
vec.push_back({n, a[n]});
for (ll i=n; i>0; i--){
for (ll j=1; j<=n; j++){
if (i==n){
dp[i % 2][j] = dp[i % 2][j-1] + 1;
continue;
}
dp[i % 2][j] = (j-1) * dp[(i+1) % 2][j] + dp[(i+1) % 2][j+1];
dp[i % 2][j] %= mod;
}
while (vec.size() and vec.back().first == i){
vals.push_back(dp[i % 2][vec.back().second]);
vec.pop_back();
}
}
reverse(vals.begin(), vals.end());
mx = 0;
for (ll i=1; i<n; i++){
ans += (a[i] - 1) * vals.back();
vals.pop_back();
ans %= mod;
mx = max(mx, a[i]);
}
ans += vals.back();
ans %= mod;
cout << ans << endl;
}
# | 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... |