# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
908587 | sofija6 | Split the sequence (APIO14_sequence) | C++14 | 1084 ms | 84316 KiB |
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 ll long long
#define MAXN 100002
#define MAXK 202
using namespace std;
ll dp[MAXN][2],pref[MAXN];
int prevv[MAXN][MAXK];
struct line
{
long double k,m;
long double Calc(ll x)
{
return k*x+m;
}
long double Intersect(line l)
{
return (m-l.m)/(l.k-k);
}
};
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll n,k,a;
cin >> n >> k;
for (int i=1;i<=n;i++)
{
cin >> a;
pref[i]=pref[i-1]+a;
}
deque<pair<line,ll> > d;
for (int i=1;i<n;i++)
dp[i][0]=pref[i]*(pref[n]-pref[i]);
for (int j=2;j<=k;j++)
{
d.clear();
d.push_front({{-pref[j-1],dp[j-1][0]},j-1});
for (int i=j;i<n;i++)
{
while (d.size()>1)
{
ll sz=d.size();
if (d[sz-1].first.Calc(pref[n]-pref[i])<=d[sz-2].first.Calc(pref[n]-pref[i]))
d.pop_back();
else
break;
}
dp[i][1]=d.back().first.Calc(pref[n]-pref[i])+pref[i]*(pref[n]-pref[i]);
prevv[i][j]=d.back().second;
if (pref[i]!=pref[i-1])
{
line cur={-pref[i],dp[i][0]};
while (d.size()>1 && cur.Intersect(d[0].first)>=d[0].first.Intersect(d[1].first))
d.pop_front();
d.push_front({cur,i});
}
}
d.clear();
for (int i=1;i<n;i++)
dp[i][0]=dp[i][1];
}
ll maxx=LLONG_MIN,pos=0;
for (int i=k;i<n;i++)
{
if (dp[i][0]>=maxx)
{
maxx=dp[i][0];
pos=i;
}
}
cout << maxx << "\n";
for (int i=k;i>0;i--)
{
cout << pos << " ";
pos=prevv[pos][i];
}
return 0;
}
/**
7 3
4 1 3 4 0 2 3
**/
Compilation message (stderr)
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |