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 <algorithm>
#include <vector>
using namespace std;
#pragma warning (disable: 4996)
const int MAX_N = 100009;
struct Node {
long long l, r, a, b; int num;
};
bool operator<(const Node &a1, const Node &a2) {
if (a1.l < a2.l) return true;
return false;
}
Node vec[MAX_N];
class ConvexHullTrick {
public:
int sz = 0;
void init() {
for (int i = 0; i < MAX_N; i++) vec[i] = Node{ 0LL,0LL,0LL,0LL,0LL };
vec[0] = Node{ -(1LL << 60), (1LL << 60), -(1LL << 30), (1LL << 60), -1LL };
sz = 1;
}
long long cross_point(Node A, Node B) {
if (A.a == B.a) {
if (A.b < B.b) return (1LL << 59);
return -(1LL << 59);
}
// A.a < B.a, 既に val の時点で B が下回っていることを表す
long long E = A.b + (B.a - A.a) * (B.a - A.a) - B.b;
long long F = 2LL * (B.a - A.a);
if (E >= 0) return B.a - E / F;
E *= -1;
return B.a + (E + F - 1) / F;
}
void add(Node B, int id) {
while (sz >= 1) {
long long P = cross_point(vec[sz - 1], B);
if (P > vec[sz - 1].l) {
vec[sz - 1].r = P - 1;
vec[sz] = Node{ P, (1LL << 60), B.a, B.b, (long long)id }; sz++;
break;
}
else sz--;
}
}
pair<long long, int> get_minimum(long long pos) {
int pos1 = lower_bound(vec, vec + sz, Node{ pos + 1LL, -(1LL << 61), 0LL, 0LL, 0LL }) - vec; pos1--;
return make_pair(vec[pos1].b + (vec[pos1].a - pos) * (vec[pos1].a - pos), vec[pos1].num);
}
};
long long N, K, A1[100009], A2[100009], prev_[100009], dp[100009]; int pre[209][100009];
long long ranged(int l, int r) {
long long V1 = A1[r] - A1[l - 1]; V1 *= V1;
return V1;
}
long long solve() {
prev_[0] = 0; for (int i = 1; i <= N; i++) prev_[i] = (1LL << 60);
for (int i = 0; i < K; i++) {
ConvexHullTrick G; G.init(); bool flag = false;
for (int j = 0; j <= N; j++) {
if (flag == true) {
pair<long long, int> F = G.get_minimum(A1[j]);
dp[j] = F.first;
pre[i + 1][j] = F.second;
}
if (prev_[j] != (1LL << 60)) { G.add(Node{ -(1LL << 60), (1LL << 60), A1[j], prev_[j], j }, j); flag = true; }
}
for (int j = 0; j <= N; j++) { prev_[j] = dp[j]; dp[j] = (1LL << 60); }
}
return prev_[N];
}
int main() {
scanf("%lld%lld", &N, &K); K++;
for (int i = 1; i <= N; i++) { scanf("%lld", &A1[i]); A2[i] = A1[i] * A1[i]; }
for (int i = 1; i <= N; i++) { A1[i] += A1[i - 1]; A2[i] += A2[i - 1]; }
cout << (ranged(1, N) - solve()) / 2LL << endl;
vector<int>T; int cx = K, cy = N;
while (cx >= 1) {
if (cx < K) T.push_back(cy);
cy = pre[cx][cy]; cx--;
}
for (int i = T.size() - 1; i >= 0; i--) { printf("%d", T[i]); if (i) printf(" "); }
printf("\n");
return 0;
}
Compilation message (stderr)
sequence.cpp:5:0: warning: ignoring #pragma warning [-Wunknown-pragmas]
#pragma warning (disable: 4996)
sequence.cpp: In member function 'void ConvexHullTrick::add(Node, int)':
sequence.cpp:48:47: warning: narrowing conversion of '(long long int)id' from 'long long int' to 'int' inside { } [-Wnarrowing]
vec[sz] = Node{ P, (1LL << 60), B.a, B.b, (long long)id }; sz++;
^~~~~~~~~~~~~
sequence.cpp: In function 'int main()':
sequence.cpp:87:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld", &N, &K); K++;
~~~~~^~~~~~~~~~~~~~~~~~~~
sequence.cpp:88:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i = 1; i <= N; i++) { scanf("%lld", &A1[i]); A2[i] = A1[i] * A1[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... |