# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1023539 | daffuwu | Split the sequence (APIO14_sequence) | C++14 | 531 ms | 84428 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;
#define fr first
#define sc second
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, k, opt[100002][202], ps[100069];
long long dp[100069][2];
struct line
{
int m;
long long c;
int id;
long long f(int x)
{
return 1ll*m*x+c;
}
double intersect(line oth)
{
return 1.0*(c-oth.c)/(oth.m-m);
}
} cur;
vector<line> vc;
int main()
{
int i, j, ii, c;
scanf("%d%d", &n, &k);
k++;
for (i=1; i<=n; i++)
{
scanf("%d", ps+i);
ps[i] += ps[i-1];
}
for (c=2; c<=k; c++)
{
for (i=c, j=0; i<=n; i++)
{
vc.push_back({ps[i-1], dp[i-1][(c+1)%2]-1ll*ps[i-1]*ps[i-1], i-1});
for (; j+1<vc.size() && vc[j].f(ps[i])<=vc[j+1].f(ps[i]); j++);
j = min(j, (int)vc.size()-1);
dp[i][c%2] = vc[j].f(ps[i]);
opt[i][c] = vc[j].id;
cur = {ps[i], dp[i][(c+1)%2]-1ll*ps[i]*ps[i], i};
for (; vc.size()>=2 && vc[vc.size()-2].intersect(vc.back())>=vc.back().intersect(cur); vc.pop_back());
}
vc.clear();
}
printf("%lld\n", dp[n][k%2]);
for (i=n, c=k; c>=2; i=opt[i][c], c--)
{
printf("%d ", opt[i][c]);
}
printf("\n");
}
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... |