Submission #291152

#TimeUsernameProblemLanguageResultExecution timeMemory
291152thiago4532Bigger segments (IZhO19_segments)C++17
0 / 100
3 ms640 KiB
#include <bits/stdc++.h>
#define int int64_t

using namespace std;
const int maxn = 5e5 + 10;
typedef long long ll;
int dp[maxn], bb[maxn];
int n, v[maxn], pref[maxn];

int32_t main() {
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> v[i];
		pref[i] = pref[i-1] + v[i];
	}
	for (int i = 1; i <= n; i++) {
		bb[i] = max(bb[i], bb[i-1]);
		dp[i] = dp[ bb[i] ] + 1;

		int soma = pref[i] - pref[bb[i]];
		
		int ini = i+1, fim = n, meio, best;
		while (ini <= fim) {
			meio = (ini + fim) >> 1;
			if (pref[meio] + pref[ bb[i] ] >= 2*pref[i]) {
				fim = meio-1;
				best = meio;
			}else
				ini = meio+1;
		}
		bb[best] = i;
	}
	cout << dp[n] << "\n";
	return 0;
}

Compilation message (stderr)

segments.cpp: In function 'int32_t main()':
segments.cpp:21:7: warning: unused variable 'soma' [-Wunused-variable]
   21 |   int soma = pref[i] - pref[bb[i]];
      |       ^~~~
segments.cpp:32:12: warning: 'best' may be used uninitialized in this function [-Wmaybe-uninitialized]
   32 |   bb[best] = i;
      |   ~~~~~~~~~^~~
#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...