Submission #1121694

#TimeUsernameProblemLanguageResultExecution timeMemory
1121694vjudge1Calvinball championship (CEOI15_teams)C++17
100 / 100
652 ms724 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define endl "\n"

using namespace std;
using namespace __gnu_pbds;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T, typename key = less_equal<T>>
using ordered_set = tree<T, null_type, key, rb_tree_tag, tree_order_statistics_node_update>;
const ll mod = 1e6 + 7;
void solve()
{
    ll n;
    cin >> n;
    ll a[n + 1];
    for (ll i = 1; i <= n; i++) cin >> a[i];
    int dp[2][n + 2][2];
    for (ll i = 0; i < 2; i++)
    for (ll j = 0; j <= n + 1; j++) dp[i][j][0] = dp[i][j][1] = 0;
    dp[0][1][0] = 1;
    for (ll i = 1; i <= n; i++)
    {
        for (ll j = 1; j <= n; j++) dp[i & 1][j][0] = dp[i & 1][j][1] = 0;
        for (ll j = 1; j <= n; j++)
        {
            if (a[i] - j <= 1)
                dp[i & 1][max(a[i], j)][0] += dp[(i - 1) & 1][j][0];
            dp[i & 1][j][1] += min(j, a[i] - 1) * dp[(i - 1) & 1][j][0] % mod;
            if (j + 1 < a[i])
                dp[i & 1][j + 1][1] += dp[(i - 1) & 1][j][0];
            dp[i & 1][j][1] += j * dp[(i - 1) & 1][j][1] % mod;
            dp[i & 1][j + 1][1] += dp[(i - 1) & 1][j][1];
        }
        for (ll j = 1; j <= n; j++) dp[i & 1][j][0] %= mod, dp[i & 1][j][1] %= mod;
    }
    ll ans = 0;
    for (ll i = 1; i <= n; i++) ans += dp[n & 1][i][1];
    cout << (ans + 1) % mod << endl;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll t = 1;
    // precomp();
    // cin >> t;
    for (ll cs = 1; cs <= t; cs++)
        solve();
    // cerr << "\nTime elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...