Submission #497919

#TimeUsernameProblemLanguageResultExecution timeMemory
497919ZielBigger segments (IZhO19_segments)C++17
27 / 100
1 ms352 KiB
/**
 * LES GREATEABLES BRO TEAM
**/

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
#define sz(x) (int)x.size()
const bool FLAG = false;
void setIO(const string &f = "");
#define int ll
const int N = 5e5 + 137;
ll a[N], dp[N], last[N], pref[N];

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

    dp[1] = 1;
    last[1] = 1;
    for (int j = 1; j <= n; j++) {
    	if (dp[j] < dp[j - 1]) {
    		dp[j] = dp[j - 1];
    		last[j] = last[j - 1];
    	}
    	int right = 2 * pref[j] - pref[last[j] - 1];

    	int lo = j, hi = n, res = -1;
    	while (lo <= hi) {
    		int mid = (lo + hi) / 2;

    		if (pref[mid] >= right) {
    			res = mid;
    			hi = mid - 1;
    		} else
    			lo = mid + 1;
    	}
    	if (res != -1) {
    		if (dp[res] <= dp[j] + 1) {
    			dp[res] = dp[j] + 1;
    			last[res] = j + 1;
	    	}
	    }
    }
    cout << dp[n];
}

signed main() {
    setIO();
    
    int tt = 1;
    if (FLAG) {
    	cin >> tt;
    }
    while (tt--) {
    	solve();
    }
    
    return 0;
}

void setIO(const string &f) {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen((f + ".in").c_str(), "r")) {
        freopen((f + ".in").c_str(), "r", stdin);
        freopen((f + ".out").c_str(), "w", stdout);
    }
}

Compilation message (stderr)

segments.cpp: In function 'void setIO(const string&)':
segments.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         freopen((f + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
segments.cpp:74:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |         freopen((f + ".out").c_str(), "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...