This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
//#include <ext/pb_ds/detail/standard_policies.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
using namespace std;
//using namespace __gnu_pbds;
//template <typename T> using ordered_set = tree <T, null_type, less < T >, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 gen(time(0));
#define ll long long
#define ld long double
#define pb push_back
#define F first
#define S second
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define sz(a) int32_t(a.size())
#define endl '\n'
//#define int long long
const int N = 5e5 + 100;
const int M = 31;
const int mod = 1e9 + 7;
const ll inf = 1e18 + 7;
ll pref[N];
vector < pair < int, ll > > add[N];
set < pair < int, ll > > st;
pair < int, ll > dp[N];
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL
int n;
cin >> n;
int ans = 1; ll ls;
for(int i = 0; i < n; i++)
{
int x;
cin >> x;
if (i) pref[i] = pref[i - 1];
else ls = x;
pref[i] += x;
}
add[0].pb({1, 0});
for(int i = 0; i < n; i++)
{
for(auto to: add[i])
st.insert(to);
dp[i] = *st.rbegin();
dp[i].S = pref[i] - dp[i].S;
int l = i + 1, r = n - 1, gr = n;
while (l <= r)
{
int mid = (l + r) / 2;
ll cnt = pref[mid] - pref[i];
if (cnt >= dp[i].S)
{
gr = mid;
r = mid - 1;
}
else l = mid + 1;
}
add[gr].pb({dp[i].F + 1, dp[i].S});
}
cout << dp[n - 1].F;
}
Compilation message (stderr)
segments.cpp: In function 'int32_t main()':
segments.cpp:54:9: warning: unused variable 'ans' [-Wunused-variable]
54 | int ans = 1; ll ls;
| ^~~
segments.cpp:54:21: warning: variable 'ls' set but not used [-Wunused-but-set-variable]
54 | int ans = 1; ll ls;
| ^~
# | 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... |