This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
Author: Cadocx
Codeforces: https://codeforces.com/profile/Kadoc
VNOJ: oj.vnoi.info/user/Cadoc
*/
#include <bits/stdc++.h>
using namespace std;
// input/output
#define fastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define el cout << '\n'
#define execute cerr << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << "s"
// #pragma GCC optimize("O2")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
//data type
#define ll long long
#define ull unsigned long long
#define pii pair<int, ll>
#define pll pair<ll, ll>
#define piv pair<int, vector<int>>
#define vi vector<int>
#define vl vector<ll>
#define vc vector<char>
//STL
#define sz(x) (int)(x).size()
#define for1(i,l,r) for(auto i = l; i <= r; i++)
#define for2(i,r,l) for(auto i = r; i >= l; i--)
#define forin(i,a) for(auto i : a)
#define pb push_back
#define eb emplace_back
#define pf push_front
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
//bitmask
#define bitcnt(n) __builtin_popcount(n)
#define mask(i) (1 << (i))
#define bit(n, i) (((n) >> (i)) & 1)
#define set_on(n, i) ((n) | mask(i))
#define set_off(n, i) ((n) & ~mask(i))
//constant
#define N 100005
#define MOD 1000230007
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define base 31
#define Kadoc 0
bool maximize(int &a, int b){
if(a < b) return a = b, 1;
return 0;
}
int n, a[N];
ll s[N];
void Init(){
cin >> n;
for(int i=1; i<=n; ++i) cin >> a[i];
}
bool checkSubtask(){
return n <= 10000;
}
namespace subtask{
pii f[N];
void solve(){
for(int i=1; i<=n; ++i) s[i] = s[i-1] + a[i];
for(int i=1; i<=n; ++i){
for(int j=0; j<i; ++j){
if(s[i] >= s[j] + f[j].se){
if(maximize(f[i].fi, f[j].fi + 1)) f[i].se = s[i] - s[j];
else if(f[i].fi == f[j].fi + 1) f[i].se = min(f[i].se, s[i] - s[j]);
}
}
}
cout << f[n].fi; el;
}
}
namespace AC{
pii f[N];
int find(int l, int r){
int id = l-1;
int ans = -1;
while(l <= r){
int m = (l+r) >> 1;
if(s[m] - s[id] >= f[id].se) ans = m, r = m-1;
else l = m+1;
}
return ans;
}
void solve(){
for(int i=1; i<=n; ++i) s[i] = s[i-1] + a[i];
for(int i=1; i<=n; ++i) f[i] = pii(1, s[i]);
for(int i=1; i<n; ++i){
if(maximize(f[i+1].fi, f[i].fi)) f[i+1].se = f[i].se + a[i+1];
else if(f[i+1].fi == f[i].fi + 1) f[i+1].se = min(f[i+1].se, f[i].se + a[i+1]);
int x = find(i+1, n);
if(maximize(f[x].fi, f[i].fi + 1)) f[x].se = s[x] - s[i];
else if(f[x].fi == f[i].fi + 1) f[x].se = min(f[x].se, s[x] - s[i]);
}
cout << f[n].fi; el;
}
}
void Solve(){
if(checkSubtask()) return subtask::solve();
AC::solve();
}
int main(){
#define NAME "TASK"
if(fopen(NAME".inp", "r")){
freopen(NAME".inp", "r", stdin);
freopen(NAME".out", "w", stdout);
}
fastIO;
if(Kadoc){
int tc; cin >> tc;
while(tc--){
Init();
Solve();
}
} else{
Init();
Solve();
}
}
Compilation message (stderr)
segments.cpp: In function 'int main()':
segments.cpp:127:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | freopen(NAME".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
segments.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
128 | freopen(NAME".out", "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... |