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>
#define gsn "Bigger segments"
#define ll long long
#define pii pair<int, ll>
#define gb(i, j) ((i >> j) & 1)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r)
{
return uniform_int_distribution<ll> (l, r) (rng);
}
const int maxn = 500005;
const ll mx = 500000000000000;
int n;
int a[maxn];
ll s[maxn];
pii dp[maxn];
map<ll, pii> bit;
void update(ll x, pii val)
{
while(x <= mx)
{
bit[x] = max(bit[x], val);
x += (x & -x);
}
}
pii get(ll x)
{
pii best = {0, 1e18};
while(x)
{
best = max(best, bit[x]);
x -= (x & -x);
}
return best;
}
void solve()
{
cin >> n;
fi(i, 1, n) cin >> a[i], s[i] = s[i - 1] + a[i];
fi(i, 1, n) dp[i] = {0, 1e18};
dp[0] = {0, 0};
update(dp[0].second + s[0] + 1, make_pair(dp[0].first + 1, s[0] + 1));
fi(i, 1, n)
{
pii best = get(s[i] + 1);
dp[i] = {best.first, s[i] + 1 - best.second};
update(dp[i].second + s[i] + 1, make_pair(dp[i].first + 1, s[i] + 1));
// fi(j, 0, i - 1)
// {
// if(s[i] - s[j] >= dp[j].second)
// {
// if(dp[j].first + 1 >= dp[i].first)
// {
// dp[i].first = dp[j].first + 1;
// dp[i].second = min(dp[i].second, s[i] - s[j]);
// }
// }
// }
}
cout << dp[n].first;
}
int main()
{
if(fopen(gsn".inp", "r"))
{
freopen(gsn".inp", "r", stdin);
freopen(gsn".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int nTest = 1;
// cin >> nTest;
while(nTest --)
{
solve();
}
/// ------------------check time!-----------------///
cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
Compilation message (stderr)
segments.cpp: In function 'int main()':
segments.cpp:84:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | freopen(gsn".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
segments.cpp:85:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | freopen(gsn".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... |