Submission #682114

#TimeUsernameProblemLanguageResultExecution timeMemory
682114opPOMoney (IZhO17_money)C++17
45 / 100
1565 ms41572 KiB
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define int long long
#define f first
#define s second
#define pb push_back
#define ld long double
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vec vector

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using oset = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;

mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
const ld eps = 1e-6;
const int mod = 998244353;
const int oo = 2e9;
const ll OO = 2e18;
const int N = 2e5 + 10;
int n;

vec<int> merge(vec<int> a, vec<int> b)
{
    reverse(all(b));
    for (int i = 0; i < sz(b) - 1; i++) if (b[i] > b[i + 1]) return {};
    if (a.empty()) return b;
    for (int i = 0; i <= sz(a); i++)
    {
        int prv = (!i ? -1 : a[i - 1]);
        int nxt = (i == sz(a) ? OO : a[i]);
        if (b[0] >= prv && b.back() <= nxt)
        {
            vec<int> ret;
            for (int j = 0; j < i; j++) ret.pb(a[j]);
            for (int j = 0; j < sz(b); j++) ret.pb(b[j]);
            for (int j = i; j < sz(a); j++) ret.pb(a[j]);
            return ret;
        }
    }
    return a;
}

void solve()
{
    cin >> n;
    vec<int> a(n + 1);
    for (int i = 1; i <= n; i++) cin >> a[i];
    vec<int> dp(n + 1, OO);
    vec<vec<int>> best(n + 1);
    dp[0] = 0;
    for (int i = 1; i <= n; i++)
    {
        vec<int> b;
        for (int j = i; j >= 1; j--)
        {
            b.pb(a[j]);
            if (sz(merge(best[j - 1], b)) == i)
            {
                if (dp[j - 1] + 1 < dp[i])
                {
                    dp[i] = dp[j - 1] + 1;
                    best[i] = merge(best[j - 1], b);
                }
            }
        }
    }
    cout << dp[n];
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...