This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
template<typename T>
string tostr(const T& value) {
ostringstream oss;
oss << value;
return oss.str();
}
template<typename... Args>
string fstr(const string& format, Args... args) {
string result = format;
size_t pos = 0;
size_t argIndex = 0;
auto replaceArg = [&](const auto& arg) {
pos = result.find("{}", pos);
if (pos != string::npos) {
result.replace(pos, 2, tostr(arg));
++argIndex;
}
};
(replaceArg(args), ...);
return result;
}
#define ll long long
const int maxn = 100000;
const int maxk = 200 + 1;
const ll INF = 2000000000'000000;
int n, k;
ll p[maxn+1];
ll dp[maxn+1][2];
int pre[maxn+1][maxk+1];
struct F {
ll a, b;
F() {}
F(ll A, ll B) { if (B < 0) B *= -1, A *= -1; a = A, b = B; }
bool operator<(F o) const { return a*o.b<b*o.a; }
bool operator<=(F o) const { return a*o.b<=b*o.a; }
};
struct L {
ll m, b; int i;
ll f(ll x) { return m*x+b; }
F operator*(L o) { if (m==o.m) return F{o.b, 1}; return F{o.b-b,m-o.m}; }
};
struct CHT {
deque<L> d;
inline void clear() { d.clear(); }
void add(L l) {
while (d.size() >= 2 && (d.end()[-2]*l) <= (d.end()[-2]*d.back())) d.pop_back();
d.push_back(l);
}
L qry(ll x) {
while (d.size() >= 2 && d[1].f(x) >= d[0].f(x)) d.pop_front();
return d[0];
}
};
signed main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) { cin >> p[i]; p[i] += p[i-1]; }
fill(dp[0] + (maxn+1), dp[0] + (maxn+1)*2, -INF);
fill(dp[0], dp[0] + (maxn+1), 0);
dp[0][0] = -INF;
for (int i = 1; i <= k; i++) {
CHT h;
dp[0][i&1] = -INF;
for (int j = 1; j <= n; j++) {
h.add(L{p[j-1], dp[j-1][!(i&1)] - p[j-1]*p[j-1], j-1});
L q = h.qry(p[j]);
dp[j][i&1] = max(q.f(p[j]), -INF);
pre[j][i] = q.i;
}
}
cout << dp[n][k & 1] << '\n';
int x = pre[n][k--];
while (k >= 0) {
cout << x << ' ';
x = pre[x][k--];
}
return 0;
}
# | 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... |