//#pragma GCC optimize("O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
//#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int mod = 1e9 + 7;
const int N = 1e5 + 5;
const int K = 205;
struct Line {
ll k, b;
int pos;
ll eval(ld x) {
return k * x + b;
}
};
inline ld isec(Line &p, Line &q) {
return ((ld)p.b - q.b) / (q.k - p.k);
}
struct CHT {
int s;
Line l[N];
//int pt;
void clr() {
s = 0;// pt = 0;
}
void add(Line a) {
while(s >= 2 && isec(l[s - 2], l[s - 1]) >= isec(l[s - 1], a)) s--;//, pt = min(pt, s - 1);
l[s++] = a;
}
pair<ll, int> get(ll x) {
int lf = 1, rg = s - 1;
if(s == 1 || x <= isec(l[0], l[1])) lf = 0;
else while(lf < rg) {
int md = (lf + rg + 1) >> 1;
if(isec(l[md - 1], l[md]) <= x) lf = md;
else rg = md - 1;
}
return { l[lf].eval(x), l[lf].pos };
}
//
// ll get(ll x) {
// while(pt != s - 1 && isec(l[pt], l[pt + 1]) <= x) pt++;
// return l[pt].eval(x);
// }
};
int a[N];
ll pref[N];
ll dp[K][N];
int way[K][N];
CHT cht;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
}
for (int i = 2; i <= n; i++) {
int pos = upper_bound(pref + 1, pref + i + 1, pref[i] / 2) - pref - 1;
for (int j = max(1, pos - 1); j <= min(pos + 1, i - 1); j++) {
if ((pref[i] - pref[j]) * pref[j] >= dp[1][i]) {
dp[1][i] = (pref[i] - pref[j]) * pref[j];
way[1][i] = j;
}
}
}
for (int i = 2; i <= k; i++) {
cht.clr();
for (int j = 1; j <= n; j++) {
if (j > i) {
auto p = cht.get(pref[j]);
dp[i][j] = p.first;
way[i][j] = p.second;
}
cht.add({ pref[j], dp[i - 1][j] - pref[j] * pref[j], j });
}
}
cout << dp[k][n] << '\n';
int kek = n;
vector<int> ans;
for (int i = k; i > 0; i--) {
ans.push_back(way[i][kek]);
kek = way[i][kek];
}
reverse(ans.begin(), ans.end());
for (int i : ans) {
cout << i << ' ';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
2 ms |
504 KB |
contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 1 == 1 |
7 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 1 == 1 |
8 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 1 == 1 |
9 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 100400096 == 100400096 |
10 |
Correct |
2 ms |
380 KB |
contestant found the optimal answer: 900320000 == 900320000 |
11 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 3698080248 == 3698080248 |
12 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 3200320000 == 3200320000 |
13 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 140072 == 140072 |
14 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 376041456 == 376041456 |
15 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 805 == 805 |
16 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 900189994 == 900189994 |
17 |
Incorrect |
2 ms |
376 KB |
contestant didn't find the optimal answer: 899899988 < 999919994 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
contestant didn't find the optimal answer: 794448 < 1093956 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
contestant didn't find the optimal answer: 413436534 < 610590000 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
376 KB |
contestant didn't find the optimal answer: 21503326 < 21503404 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
1144 KB |
contestant didn't find the optimal answer: 402788319 < 1818678304 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
169 ms |
7388 KB |
contestant didn't find the optimal answer: 18583553952 < 19795776960 |
2 |
Halted |
0 ms |
0 KB |
- |