#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <fstream>
#include <iterator>
#include <set>
#include <map>
#include <unordered_map>
#include <iomanip>
#include <cctype>
#include <string>
#include <cassert>
#include <set>
#include <bitset>
#include <unordered_set>
#include <numeric>
#define all(a) a.begin(), a.end()
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define ppi pair<int,pair<int,int>>
#define int int64_t
using namespace std;
// /\_/\
// (= ._.)
// / > \>
// encouraging cat
const int INF = 10000000000000000;
//const int mod = 1000000007;
const int mod = 998244353;
const int MAXN = 200005;
//ifstream fin('xor.in');
//ofstream fout('xor.out');
vector<int> a, pref;
int rq(int l,int r)
{
if (l == 0)
{
return pref[r];
}
return pref[r] - pref[l - 1];
}
signed main()
{
int n;
cin >> n;
int s = 0;
a.resize(n);
pref.resize(n);
for (int i = 0;i < n;i++)
{
cin >> a[i];
s += a[i];
pref[i] = s;
}
vector<int> val(n,0);
vector<int> nbseg(n,0);
val[0] = a[0];
nbseg[0] = 1;
for (int i = 1;i < n;i++)
{
int lo = 1, hi = i, mid = (lo + hi + 1) / 2;
while (lo < hi)
{
mid = (lo + hi + 1) / 2;
if (val[mid - 1] <= rq(mid, i))
{
lo = mid;
}
else
{
hi = mid - 1;
}
}
if (val[lo - 1] > rq(lo, i))
{
val[i] = val[i - 1] + a[i];
nbseg[i] = nbseg[i - 1];
}
else
{
val[i] = rq(lo,i);
nbseg[i] = nbseg[lo - 1] + 1;
}
}
cout << nbseg[n - 1] << '\n';
return 0;
}
# | 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... |