제출 #1348366

#제출 시각아이디문제언어결과실행 시간메모리
1348366killerzaluuBigger segments (IZhO19_segments)C++20
100 / 100
54 ms14128 KiB
#include <bits/stdc++.h>
using namespace std;

const int N = 500000 + 5;

int n;
long long a[N], s[N];
int dp[N], p[N], best[N], from[N];

int better(int x, int y) {
    if (dp[x] != dp[y]) return dp[x] > dp[y] ? x : y;
    return s[x] > s[y] ? x : y;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        s[i] = s[i - 1] + a[i];
    }

    best[1] = 0;

    for (int i = 1; i <= n; i++) {
        best[i] = better(best[i], best[i - 1]);

        p[i] = best[i];
        dp[i] = dp[p[i]] + 1;

        long long need = 2 * s[i] - s[p[i]];
        int j = lower_bound(s + 1, s + n + 1, need) - s;

        if (j <= n) {
            best[j] = better(best[j], i);
        }
    }

    cout << dp[n] << '\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...