/*
dp+convex hull trick
only add lines to the end of the hull bc slopes increase bc pre[i] increases
answer for i is >= answer for i-1
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(i, a, b) for(int i=a; i<b; i++)
#define ffi For(i, 0, N)
#define ffj For(j, 0, K+1)
#define ffa ffi ffj
#define s <<" "<<
#define c <<" : "<<
#define w cout
#define e "\n"
#define pb push_back
#define mp make_pair
#define a first
#define b second
//#define int ll
//500,000,000 operations
const int MAXN = 100000;
//Global Variables
int from[MAXN][201], N, K;
ll pre[MAXN], BIG=1000000000, dp[MAXN][2];
//deque<int> hull; ///(ind)
int hull[MAXN], l=1, r;
struct Line { ///Ax+B
ll A, B, l, r, yb, yl;
Line() {A=0; B=0;}
Line(int j, int line) {
A = pre[j];
B = dp[j][line%2]-pre[j]*pre[j];
yb = A*BIG+B;
//w<< j s ":" s A s B<<e;
//w<< dp[j][line%2]-pre[j]*pre[j] s B<<e;
}
}val[MAXN];
void put(int ind, int line, ll X) {
val[ind] = Line(ind, line);
ll A = val[ind].A, B = val[ind].B;
while (l != r+1) {
int j = hull[r];
if (val[j].yl <= A*val[j].l+B) r--;
else break;
}
if (l != r+1) {
int last = hull[r];
if (val[last].yb < val[ind].yb) {
/// split the range
ll x = (val[last].B - B)/(A - val[last].A);
r++; hull[r] = ind;
val[last].r = x;
val[ind].l = x+1;
val[ind].yl = A*(x+1) + B;
val[ind].r = BIG;
}
}
while (l != r+1 && val[hull[l]].r < X) l++;
if (l==r+1) {
r++; hull[r] = ind;
///range: 0-BIG
val[ind].l = 0;
val[ind].yl = B;
val[ind].r = BIG;
}
}
main() {
//ifstream cin("test.in");
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> K;
cin >> pre[0];
For (i, 1, N) {int a; cin >> a; pre[i] = a+pre[i-1];}
For (j, 1, K+1) {
l = 1; r = 0;
put(j-1, j-1, pre[j]);
For (i, j, N) {
int k = hull[l];
dp[i][j%2] = val[k].A*pre[i]+val[k].B;
from[i][j] = k;
if (i != N-1) put(i, j-1, pre[i+1]);
//w<< l s r<<e;
//w<< "dp["<< i+1 <<"]["<<j<<"]:" s dp[i][j%2]<<e;
}
}
//ffj {ffi w<< from[i][j]+1<< " "; w<<e;}w<<e;
//ffi w<< pre[i]<<e;
w<< dp[N-1][K%2]<<e; int at = N-1; for (int j=K; j>=1; j--) {w<< from[at][j]+1<< " ";at = from[at][j];}w<<e;
}
Compilation message
sequence.cpp:73:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
6 ms |
5212 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
6 ms |
5212 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
4 ms |
5364 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
7 ms |
5392 KB |
contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Correct |
7 ms |
5392 KB |
contestant found the optimal answer: 1 == 1 |
7 |
Correct |
7 ms |
5392 KB |
contestant found the optimal answer: 1 == 1 |
8 |
Correct |
7 ms |
5392 KB |
contestant found the optimal answer: 1 == 1 |
9 |
Correct |
7 ms |
5392 KB |
contestant found the optimal answer: 100400096 == 100400096 |
10 |
Correct |
7 ms |
5392 KB |
contestant found the optimal answer: 900320000 == 900320000 |
11 |
Correct |
7 ms |
5392 KB |
contestant found the optimal answer: 3698080248 == 3698080248 |
12 |
Correct |
6 ms |
5392 KB |
contestant found the optimal answer: 3200320000 == 3200320000 |
13 |
Correct |
6 ms |
5392 KB |
contestant found the optimal answer: 140072 == 140072 |
14 |
Correct |
6 ms |
5392 KB |
contestant found the optimal answer: 376041456 == 376041456 |
15 |
Correct |
6 ms |
5472 KB |
contestant found the optimal answer: 805 == 805 |
16 |
Correct |
6 ms |
5472 KB |
contestant found the optimal answer: 900189994 == 900189994 |
17 |
Correct |
6 ms |
5472 KB |
contestant found the optimal answer: 999919994 == 999919994 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5472 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
6 ms |
5472 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
7 ms |
5472 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
6 ms |
5472 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
6 ms |
5472 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
6 ms |
5472 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
7 ms |
5472 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
7 ms |
5472 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
7 ms |
5472 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
6 ms |
5472 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5500 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
6 ms |
5500 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
6 ms |
5500 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
6 ms |
5500 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
6 ms |
5500 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Correct |
7 ms |
5500 KB |
contestant found the optimal answer: 107630884 == 107630884 |
7 |
Correct |
6 ms |
5500 KB |
contestant found the optimal answer: 475357671774 == 475357671774 |
8 |
Correct |
6 ms |
5500 KB |
contestant found the optimal answer: 193556962 == 193556962 |
9 |
Correct |
6 ms |
5500 KB |
contestant found the optimal answer: 482389919803 == 482389919803 |
10 |
Correct |
6 ms |
5500 KB |
contestant found the optimal answer: 490686959791 == 490686959791 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
6140 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
6 ms |
6140 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
12 ms |
6140 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
6 ms |
6140 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
13 ms |
6140 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
12 ms |
6176 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
13 ms |
6176 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
12 ms |
6176 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
8 ms |
6176 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
9 ms |
6176 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
13436 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
16 ms |
13436 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
103 ms |
13508 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
15 ms |
13508 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
73 ms |
13564 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
80 ms |
13564 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
87 ms |
13564 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
70 ms |
13564 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
73 ms |
13564 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
96 ms |
13572 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
86672 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
98 ms |
86676 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Execution timed out |
2100 ms |
86984 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |