#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
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 |
1 |
Correct |
6 ms |
4224 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
7 ms |
4352 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
7 ms |
4352 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
8 ms |
4224 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
8 ms |
4352 KB |
contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Correct |
7 ms |
4352 KB |
contestant found the optimal answer: 1 == 1 |
7 |
Correct |
7 ms |
4352 KB |
contestant found the optimal answer: 1 == 1 |
8 |
Correct |
6 ms |
4352 KB |
contestant found the optimal answer: 1 == 1 |
9 |
Correct |
8 ms |
4224 KB |
contestant found the optimal answer: 100400096 == 100400096 |
10 |
Correct |
7 ms |
4352 KB |
contestant found the optimal answer: 900320000 == 900320000 |
11 |
Correct |
7 ms |
4324 KB |
contestant found the optimal answer: 3698080248 == 3698080248 |
12 |
Correct |
7 ms |
4352 KB |
contestant found the optimal answer: 3200320000 == 3200320000 |
13 |
Correct |
9 ms |
4324 KB |
contestant found the optimal answer: 140072 == 140072 |
14 |
Correct |
7 ms |
4224 KB |
contestant found the optimal answer: 376041456 == 376041456 |
15 |
Correct |
6 ms |
4224 KB |
contestant found the optimal answer: 805 == 805 |
16 |
Correct |
7 ms |
4224 KB |
contestant found the optimal answer: 900189994 == 900189994 |
17 |
Correct |
7 ms |
4224 KB |
contestant found the optimal answer: 999919994 == 999919994 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4224 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
7 ms |
4224 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
21 ms |
4480 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
6 ms |
4224 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
10 ms |
4352 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
8 ms |
4224 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
15 ms |
4320 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
11 ms |
4352 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
8 ms |
4352 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
9 ms |
4352 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4352 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
8 ms |
4224 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
63 ms |
5172 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
7 ms |
4224 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
58 ms |
4984 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Correct |
59 ms |
5240 KB |
contestant found the optimal answer: 107630884 == 107630884 |
7 |
Correct |
65 ms |
5240 KB |
contestant found the optimal answer: 475357671774 == 475357671774 |
8 |
Correct |
19 ms |
4476 KB |
contestant found the optimal answer: 193556962 == 193556962 |
9 |
Correct |
16 ms |
4480 KB |
contestant found the optimal answer: 482389919803 == 482389919803 |
10 |
Correct |
25 ms |
4572 KB |
contestant found the optimal answer: 490686959791 == 490686959791 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4352 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
6 ms |
4352 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
61 ms |
5880 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
8 ms |
4352 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
69 ms |
6008 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
59 ms |
5624 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
64 ms |
5880 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
80 ms |
6008 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
20 ms |
4608 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
30 ms |
4984 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
4736 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
10 ms |
4736 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
161 ms |
13176 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
10 ms |
4736 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
136 ms |
9920 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
175 ms |
10732 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
169 ms |
11116 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
128 ms |
10052 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
150 ms |
10744 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
177 ms |
12380 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
8956 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
39 ms |
8952 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Correct |
1321 ms |
86264 KB |
contestant found the optimal answer: 497313449256899208 == 497313449256899208 |
4 |
Correct |
55 ms |
9464 KB |
contestant found the optimal answer: 374850090734572421 == 374850090734572421 |
5 |
Correct |
1538 ms |
86392 KB |
contestant found the optimal answer: 36183271951 == 36183271951 |
6 |
Correct |
1202 ms |
62792 KB |
contestant found the optimal answer: 51629847150471 == 51629847150471 |
7 |
Correct |
1087 ms |
67180 KB |
contestant found the optimal answer: 124074747024496432 == 124074747024496432 |
8 |
Correct |
939 ms |
56068 KB |
contestant found the optimal answer: 309959349080800 == 309959349080800 |
9 |
Correct |
960 ms |
63044 KB |
contestant found the optimal answer: 124113525649823701 == 124113525649823701 |
10 |
Correct |
1350 ms |
78540 KB |
contestant found the optimal answer: 124309619349406845 == 124309619349406845 |