제출 #407429

#제출 시각아이디문제언어결과실행 시간메모리
407429534351Cigle (COI21_cigle)C++17
48 / 100
1018 ms67584 KiB
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
    if (a > b) a = b;
}

template<class T, class U>
void ckmax(T &a, U b)
{
    if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()

const int MAXN = 5013;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N;
int arr[MAXN], pref[MAXN];
int dp[MAXN][MAXN];
int ans;
// int seg[4 * MAXN], lazy[4 * MAXN];
//
// void build(int w, int L, int R, int k)
// {
//     lazy[w] = 0;
//     if (L == R)
//     {
//         seg[w] = dp[L][k];
//         return;
//     }
//     int mid = (L + R) >> 1;;
//     build(w << 1, L, mid, k);
//     build(w << 1 | 1, mid + 1, R, k);
//     seg[w] = max(seg[w << 1], seg[w << 1 | 1]);
// }
// void push(int w, int L, int R)
// {
//     if (!lazy[w]) return;
//     seg[w] += lazy[w];
//     if (L != R)
//     {
//         lazy[w << 1] += lazy[w];
//         lazy[w << 1 | 1] += lazy[w];
//     }
//     lazy[w] = 0;
//     return;
// }
// void update(int w, int L, int R, int a, int b)
// {
//     push(w, L, R);
//     if (b < L || R < a)
//     {
//         return;
//     }
//     if (a <= L && R <= b)
//     {
//         lazy[w]++;
//         push(w, L, R);
//         return;
//     }
//     int mid = (L + R) >> 1;
//     update(w << 1, L, mid, a, b);
//     update(w << 1 | 1, mid + 1, R, a, b);
//     seg[w] = max(seg[w << 1], seg[w << 1 | 1]);
// }

int fen[MAXN];
void update(int idx)
{
    for (int e = idx + 1; e <= N + 1; e += e & (-e))
    {
        fen[e]++;
    }
}
int query(int idx)
{
    int res = 0;
    for (int e = idx + 1; e; e -= e & (-e))
    {
        res += fen[e];
    }
    return res;
}

int32_t main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    cout << fixed << setprecision(12);
    cerr << fixed << setprecision(4);
    cin >> N;
    FOR(i, 0, N)
    {
        cin >> arr[i];
        pref[i + 1] = pref[i] + arr[i];
    }
    // dp[0][0] = 0;
    FOR(i, 2, N + 1)
    {
        fill(fen, fen + N + 2, 0);
        set<int> cands;
        FOR(j, 1, i)
        {
            if (j == 1 || dp[j][i - 1] > dp[*cands.rbegin()][i - 1])
            {
                cands.insert(j);
            }
        }
        int best = dp[*cands.rbegin()][i - 1] + query(N - *cands.rbegin());
        int iter = i - 1;
        FOR(j, i, N + 1)
        {
            dp[i][j] = best;
            int c = 2 * pref[i - 1] - pref[j];
            while(iter != 0 && pref[iter] > c)
            {
                iter--;
            }
            if (pref[iter] == c)
            {
                update(N - iter);
                if (N - iter <= N - *cands.rbegin())
                {
                    best++;
                }
                auto it = cands.UB(iter);
                if (it != cands.end() && it != cands.begin())
                {
                    int u = *prev(it), v = *it;
                    if (dp[u][i - 1] + query(N - u) >= dp[v][i - 1] + query(N - v))
                    {
                        cands.erase(it);
                        if (u == *cands.rbegin())
                        {
                            best = dp[u][i - 1] + query(N - u);
                        }
                    }
                }
            }
        }
    }
    FOR(i, 1, N + 1)
    {
        ckmax(ans, dp[i][N]);
    }
    cout << ans << '\n';
    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...
#Verdict Execution timeMemoryGrader output
Fetching results...