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 <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#define INF 1e18
unsigned long long Sum[100005];
unsigned long long SumSq[100005];
unsigned long long DP[100005][2];
int Last[100005][205];
typedef struct line line;
struct line
{
unsigned long long c;
double s;
int i;
};
line Lines[100005];
int L;
int cnt = 0;
int I, J;
double intersect(unsigned long long m, unsigned long long c, unsigned long long m2, unsigned long long c2)
{
// cnt++;
if (m == m2)
{
if (c2 >= c)
return -INF;
else
return INF;
}
else
return (double) (c2 - c)/(m - m2);
}
int main()
{
int N, K;
std::ios::sync_with_stdio(false);
// scanf("%d %d", &N, &K);
std::cin >> N >> K;
int n, k, i;
for (i = 0; i < N; i++)
{
// scanf("%lld", &Sum[i]);
std::cin >> Sum[i];
if (i > 0)
Sum[i] += Sum[i - 1];
SumSq[i] = Sum[i]*Sum[i];
}
unsigned long long r, c;
double s;
for (k = 1; k <= K; k++)
{
L = 1;
J = 0;
Lines[0] = {0, -INF, -1};
for (n = 0; n < N; n++)
{
while (J + 1 < L && Lines[J + 1].s <= Sum[n])
{
J++;
// cnt++;
}
DP[n][k%2] = Sum[Lines[J].i]*Sum[n] + Lines[J].c;
Last[n][k] = Lines[J].i;
c = DP[n][(k - 1)%2] - SumSq[n];
while (L > 0 && (s = intersect(Sum[Lines[L - 1].i], Lines[L - 1].c, Sum[n], c)) <= Lines[L - 1].s)
{
L--;
// cnt++;
}
if (s != INF)
{
Lines[L] = {c, s, n};
L++;
}
}
// printf("k%d\n", k);
}
// printf("%lld\n", DP[N - 1][K%2]);
std::cout << DP[N - 1][K%2] << '\n';
i = Last[N - 1][K];
k = K - 1;
std::vector<int> V;
while (k >= 0 && i != -1)
{
V.push_back(i + 1);
i = Last[i][k];
k--;
}
for (i = V.size() - 1; i >= 0; i--)
std::cout << V[i] << ' ';
// printf("%d ", V[i]);
// std::cout << "cnt" << cnt;
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:49:24: warning: unused variable 'r' [-Wunused-variable]
unsigned long long r, c;
^
# | 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... |