#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define pa pair<ll, ll>
#define fi first
#define se second
#define bit(mask, j) ((mask >> j) & 1)
#define t_test int t;cin >> t;while(t--)
const ll mod = 1e9 + 7;
const ll INF = 1e18;
inline void adm(ll &x){if(x>=mod)x%=mod;else if(x<0)x+=mod;}
//--------------------------------------------------------------------
const ll N = 500003;
pa dp[N];
ll a[N], n, s[N];
ll get_sum(ll u, ll v) {
return s[v] - s[u -1];
}
deque<ll> q;
void hbmt() {
cin >> n;
FOR(i, 1, n) {
cin >> a[i];
s[i] = s[i - 1] + a[i];
dp[i] = {-INF, INF};
}
dp[0] = {0, 0};
q.push_back(0);
ll id = 1;
FOR(i, 1, n) {
ll sum = 0, j = 0;
while(!q.empty() && dp[q.front()].se <= get_sum(q.front() + 1, i)) {
j = q.front();
dp[i] = {dp[j].fi + 1, get_sum(q.front() + 1, i)};
q.pop_front();
}
q.push_front(j);
while(!q.empty() && dp[q.back()].se >= dp[i].se) {
q.pop_back();
}
q.push_back(i);
// cout << dp[i].fi << ' ' << dp[i].se << '\n';
}
cout << dp[n].fi << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
if(fopen("hbmt.inp", "r")) {
freopen("hbmt.inp", "r", stdin);
freopen("hbmt.ans", "w", stdout);
}
// t_test
hbmt();
return 0;
}
Compilation message (stderr)
segments.cpp: In function 'int main()':
segments.cpp:57:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | freopen("hbmt.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
segments.cpp:58:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | freopen("hbmt.ans", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |