Submission #784898

#TimeUsernameProblemLanguageResultExecution timeMemory
784898fanwenBigger segments (IZhO19_segments)C++17
100 / 100
78 ms12932 KiB
/**
 *      author : pham van sam 
 *      created : 17 July 2023 (Monday)
 **/

#include <bits/stdc++.h>

using namespace std;

#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(it, s) for (__typeof(s.begin()) it = (s).begin(); it != (s).end(); ++it)

template <typename U, typename V> bool maximize(U &A, const V &B) { return (A < B) ? (A = B, true) : false; }
template <typename U, typename V> bool minimize(U &A, const V &B) { return (A > B) ? (A = B, true) : false; }

void you_make_it(void);

signed main() {

    #define TASK "segments"
    if(fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }

    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    auto start_time = chrono::steady_clock::now();

    int test = 1;
    // cin >> test;
    for (int i = 1; i <= test; ++i) {
        you_make_it();
        // cout << '\n';
    }

    auto end_time = chrono::steady_clock::now();

    cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;

    return (0 ^ 0);
}

const int MAXN = 5e5 + 5;

int N, dp[MAXN], nxt[MAXN];
long long a[MAXN];

void you_make_it() {
    cin >> N;
    FOR(i, 1, N) cin >> a[i], a[i] += a[i - 1];
    nxt[1] = 0;
    FOR(i, 1, N) {
        dp[i] = dp[nxt[i]] + 1;
        int r = lower_bound(a + 1, a + N + 1, 2 * a[i] - a[nxt[i]]) - a;
        if(r != N + 1) nxt[r] = i;
        maximize(nxt[i + 1], nxt[i]);
    }
    cout << dp[N];
}

// Dream it. Wish it. Do it.

Compilation message (stderr)

segments.cpp: In function 'int main()':
segments.cpp:27:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
segments.cpp:28:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...