//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
// #include <bits/extc++.h>
using namespace std;
#define debug(s) cout << #s << " = " << s << endl
#define all(v) (v).begin(), (v).end()
#define KeepUnique(v) (v).erase( unique(all(v)), v.end() )
#define MEMSET(a, val) memset(a, val, sizeof (a))
#define PB push_back
#define endl '\n'
typedef long long ll;
inline int myrand(int l, int r) {
int ret = rand(); ret <<= 15; ret ^= rand();
if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
return ret;
}
template <typename F, typename S>
ostream& operator << (ostream& os, const pair< F, S>& p) {
return os<<"(" <<p.first<<", "<<p.second<<")"; }
typedef pair<int, int> ii;
template<typename T> using min_pq =
std::priority_queue<T, vector<T>, greater<T> >;
//int dx[] = {-1, +0, +1, +0};
//int dy[] = {+0, +1, +0, -1};
const int maxn = 10005;
const int mod = 1000007;
int dp[2][maxn];
int32_t main () {
// freopen("in", "r", stdin);
int n;
cin >> n;
vector<int> v(n);
for(int &b : v) cin >> b;
if(n == 2) {
int ans;
if(v[0] == 1 and v[1] == 1) ans = 1;
else ans = 2;
cout << ans << endl;
return 0;
}
vector<int> pre(n, v[0]);
map<int, map<int, int>> mp;
for(int i = 1; i < n; i++) {
pre[i] = max(pre[i - 1], v[i]);
}
for(int i = 0; i <= n; i++) dp[0][i] = 1;
mp[n][pre[n - 2]] = 1;
int tog = 1;
for(int p = n - 1; p >= 0; p--) {
for(int k = 0; k <= n; k++) {
dp[tog][k] = (dp[tog ^ 1][k + 1] + 1LL * k * dp[tog ^ 1][k]) % mod;
}
if(p <= 1) mp[p][0] = dp[tog][0];
else mp[p][pre[p - 2]] = dp[tog][pre[p - 2]];
tog ^= 1;
}
int ret = 0, mx = 0;
for(int i = 0; i < n; i++) {
ret = (ret + 1LL * (v[i] - 1) * mp[i + 1][mx]) % mod;
mx = max(mx, v[i]);
}
ret++;
if(ret == mod) ret = 0;
cout << ret << endl;
}
Compilation message
teams.cpp: In function 'int myrand(int, int)':
teams.cpp:18:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
^~
teams.cpp:18:26: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
3 |
Correct |
3 ms |
492 KB |
Output is correct |
4 |
Correct |
3 ms |
492 KB |
Output is correct |
5 |
Correct |
3 ms |
492 KB |
Output is correct |
6 |
Correct |
3 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
492 KB |
Output is correct |
2 |
Correct |
3 ms |
492 KB |
Output is correct |
3 |
Correct |
3 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
528 KB |
Output is correct |
2 |
Correct |
3 ms |
548 KB |
Output is correct |
3 |
Correct |
4 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
620 KB |
Output is correct |
2 |
Correct |
2 ms |
620 KB |
Output is correct |
3 |
Correct |
3 ms |
624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
628 KB |
Output is correct |
2 |
Correct |
4 ms |
628 KB |
Output is correct |
3 |
Correct |
5 ms |
636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
764 KB |
Output is correct |
2 |
Correct |
8 ms |
764 KB |
Output is correct |
3 |
Correct |
7 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
311 ms |
2100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
2100 KB |
Output is correct |
2 |
Correct |
112 ms |
2100 KB |
Output is correct |
3 |
Correct |
127 ms |
2100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
270 ms |
2264 KB |
Output is correct |
2 |
Correct |
268 ms |
2264 KB |
Output is correct |
3 |
Correct |
283 ms |
2368 KB |
Output is correct |