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 int long long
using namespace std;
const int nmax = 500000;
int n;
int v[nmax + 5], sume[nmax + 5];
struct elem
{
int maxSeg; /// in cate segmente impart prefixul [1 ... i]
int minSum; /// suma minima pentru ultimul segment
int maxPrv; /// cel mai mare capat dreapta al segmentului dp[i].maxSeg - 1
///daca capatul stanga al ultimului segment e j, maxPrv este j - 1
};
elem dp[nmax + 5];
signed main()
{
cin >> n;
for(int i = 1; i <= n; i ++)
{
cin >> v[i];
sume[i] = sume[i - 1] + v[i];
}
/// initializare
for(int i = 1; i <= n; i ++)
{
dp[i] = {1, sume[i], 1};
}
for(int i = 2; i <= n; i ++)
{
for(int j = 1; j < i; j ++)
{
if(dp[j].minSum <= sume[i] - sume[j])
{
if(dp[j].maxSeg + 1 > dp[i].maxSeg)
{
dp[i].maxSeg = 1 + dp[j].maxSeg;
dp[i].minSum = sume[i] - sume[j];
dp[i].maxPrv = j;
}
else
{
if(dp[j].maxSeg + 1 == dp[i].maxSeg)
{
if(sume[i] - sume[j] < dp[i].minSum)
{
dp[i].minSum = sume[i] - sume[j];
dp[i].maxPrv = j;
}
}
}
}
}
}
cout << dp[n].maxSeg << '\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... |