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 <stdio.h>
#include <vector>
#include <algorithm>
#define INF 1e18
long long Sum[100005];
long long DP[100005][205];
int Last[100005][205];
typedef struct line line;
struct line
{
long long m, c;
double s;
int i;
};
std::vector<line> Lines;
long long max(long long a, long long b)
{
return (a > b)?a:b;
}
int compare(long long x, line l)
{
return x < l.s;
}
int I;
long long getLine(long long x)
{
int j = std::upper_bound(Lines.begin(), Lines.end(), x, compare) - Lines.begin() - 1;
I = Lines[j].i;
return Lines[j].m*x + Lines[j].c;
}
double intersect(long long m, long long c, long long m2, long long c2)
{
if (m == m2)
{
if (c2 > c)
return -INF;
else
return INF;
}
else
return (double) (c2 - c)/(m - m2);
}
void insertLine(int i, long long m, long long c)
{
double s;
while ((s = intersect(Lines.back().m, Lines.back().c, m, c)) < Lines.back().s)
Lines.pop_back();
if (s != INF)
Lines.push_back({m, c, s, i});
}
int main()
{
int N, K;
scanf("%d %d", &N, &K);
int n, k, i;
for (i = 0; i < N; i++)
{
scanf("%lld", &Sum[i]);
if (i > 0)
Sum[i] += Sum[i - 1];
}
Lines.push_back({0, 0, 0});
for (k = 0; k <= K; k++)
{
for (n = 0; n < N; n++)
{
DP[n][k] = getLine(Sum[n]);
Last[n][k] = I;
// for (i = 0; i < n; i++)
// {
// if (DP[i][k - 1] + Sum[i]*(Sum[n] - Sum[i]) > DP[n][k])
// {
// DP[n][k] = max(DP[n][k], DP[i][k - 1] + Sum[i]*(Sum[n] - Sum[i]));
// Last[n][k] = i;
// }
// }
// printf("DP[%d][%d] = %lld\n", n, k, DP[n][k]);
}
Lines.clear();
Lines.push_back({0, 0, 0});
for (n = 0; n < N; n++)
{
insertLine(n, Sum[n], DP[n][k] - Sum[n]*Sum[n]);
}
}
printf("%lld\n", DP[N - 1][K]);
i = Last[N - 1][K];
k = K - 1;
std::vector<int> V;
while (k >= 0)
{
V.push_back(i + 1);
i = Last[i][k];
k--;
}
for (i = V.size() - 1; i >= 0; i--)
printf("%d ", V[i]);
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:54:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &K);
~~~~~^~~~~~~~~~~~~~~~~
sequence.cpp:58:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &Sum[i]);
~~~~~^~~~~~~~~~~~~~~~~
# | 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... |