# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
331028 | leinad2 | Split the sequence (APIO14_sequence) | C++14 | 868 ms | 92764 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>
using namespace std;
typedef long long ll;
ll n, i, j, k, t, a, A[100010], dp[100010][2], chk[100010];
struct line
{
ll a, b, index;
};
vector<line>V;
double cross(const line &p, const line &q)
{
return (double)(q.b-p.b)/(p.a-q.a);
}
void push(line x)
{
if(V.size()&&V.back().a==x.a)
{
if(V.back().b<x.b)V.back().b=x.b;
return;
}
while(V.size()>=2)
{
if(cross(V.back(), V[V.size()-2])>cross(V.back(), x))V.pop_back();
else break;
}
V.push_back(x);
}
pair<ll, ll>query(ll x)
{
if(V.size()<=a)a=V.size()-1;
else while(a+1<V.size()&&cross(V[a+1], V[a])<=x)a++;
return {V[a].a*x+V[a].b, V[a].index};
}
int backtrack[100010][210];
int main()
{
for(scanf("%lld %lld", &n, &k);i++<n;)
{
scanf("%lld", &A[i]);
A[i]+=A[i-1];
}
for(j=0;j++<k;)
{
for(a=i=0;i++<n;)
{
push({A[i], dp[i][0]-A[i]*A[i], i});
pair<ll, ll>p=query(A[i]);
dp[i][1]=p.first;
backtrack[i][j]=p.second;
dp[i][0]=dp[i][1];
}
V.clear();
}
printf("%lld\n", dp[n][1]);
i=n;
j=k;
vector<int>ans;
while(j>0)
{
i=backtrack[i][j];
j--;
ans.push_back(i);
}
set<int>s;
for(i=0;++i<n;)s.insert(i);
while(ans.size())
{
if(ans.back()>=n)
{
ans.back()=*s.begin();
}
printf("%d ", ans.back());
chk[ans.back()]=1;
s.erase(ans.back());
ans.pop_back();
}
}
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... |