# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
158360 | arnold518 | Split the sequence (APIO14_sequence) | C++14 | 1753 ms | 87544 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;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5;
const int MAXK = 200;
int N, K;
ll A[MAXN+10], dp[MAXN+10], dp2[MAXN+10];
int memo[MAXN+10][MAXK+10];
struct CHT
{
void init()
{
S.clear();
pos=0;
}
struct Line { ll a, b, c; };
double cross(const Line &p, const Line &q) { return (double)(q.b-p.b)/(p.a-q.a); }
vector<Line> S;
void update(Line p)
{
if(!S.empty() && S[S.size()-1].a==p.a)
{
if(S[S.size()-1].b>=p.b) return;
else S.pop_back();
}
while(S.size()>1 && (S[S.size()-1].a!=p.a && cross(S[S.size()-1], p)<=cross(S[S.size()-2], S[S.size()-1]))) S.pop_back();
S.push_back(p);
}
int pos=0;
pll query(ll x)
{
if(S.size()<=pos) pos=S.size()-1;
else while(pos+1<S.size() && cross(S[pos], S[pos+1])<=x) pos++;
return {S[pos].a*x+S[pos].b, S[pos].c};
}
};
CHT cht;
int main()
{
int i, j;
scanf("%d%d", &N, &K);
for(i=1; i<=N; i++) scanf("%lld", &A[i]), A[i]+=A[i-1];
for(j=1; j<=K; j++)
{
cht.init();
cht.update({A[j], dp2[j]-A[j]*A[j], j});
for(i=j+1; i<=N; i++)
{
pll t=cht.query(A[i]);
dp[i]=t.first;
memo[i][j]=t.second;
cht.update({A[i], dp2[i]-A[i]*A[i], i});
}
for(i=j+1; i<=N; i++) dp2[i]=dp[i];
//for(i=1; i<=N; i++) printf("%lld ", dp[i]); printf("\n");
}
printf("%lld\n", dp[N]);
int now=N;
for(i=K; i>=1; i--)
{
now=memo[now][i];
printf("%d ", now);
}
}
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... |