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 <bits/stdc++.h>
#define ll long long
#define F first
#define S second
using namespace std;
const int N = 1e5 + 5;
ll n, k, a[N], S[N], dp[N], odp[N];
int P[N][202];
struct Line {
ll k, b, id;
ll vl(ll x) {
return k * x + b;
}
pair <ll , ll > intr(const Line& yo) {
ll one = yo.b - b;
ll two = k - yo.k;
if (two < 0) one *= -1, two *= -1;
return {one, two};
}
};
deque < Line > dq;
void insert(ll k, ll b, ll id) {
Line line = {k, b, id};
while (dq.size() > 1) {
Line linea = dq.end()[-1];
Line lineb = dq.end()[-2];
pair < ll , ll > A = lineb.intr(linea);
pair < ll , ll > B = linea.intr(line);
if (A.F * B.S >= B.F * A.S) dq.pop_back();
else break;
}
dq.push_back(line);
}
pair < ll , int > qr(ll x, int idx) {
while (dq.size() > 1 && dq[1].id < idx) {
if (dq[0].vl(x) <= dq[1].vl(x)) dq.pop_front();
else break;
}
return {dq[0].vl(x), dq[0].id};
}
main () {
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
S[i] = S[i - 1] + a[i];
}
for (int st = 1; st <= k; ++st) {
while (dq.size()) dq.pop_back();
insert(S[st - 1], odp[st - 1] - S[n] * S[st - 1], st - 1);
for (int i = st; i <= n; ++i) {
pair < ll , int > lol = qr(S[i], i);
P[i][st] = lol.S;
dp[i] = lol.F + (S[n] - S[i]) * S[i];
insert(S[i], odp[i] - S[n] * S[i], i);
odp[i] = dp[i];
}
}
vector < int > ans;
ll idx = k, res = dp[k];
for (int i = k + 1; i <= n; ++i) {
if (dp[i] >= res) {
res = dp[i];
idx = i;
}
}
cout << res << "\n";
for (int i = k; i >= 1; --i) {
ans.push_back(idx);
idx = P[idx][i];
}
reverse(ans.begin(), ans.end());
for (int i = 0; i < ans.size(); ++i) {
cout << ans[i] << " ";
}
}
Compilation message (stderr)
sequence.cpp:52:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
52 | main () {
| ^
sequence.cpp: In function 'int main()':
sequence.cpp:89:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (int i = 0; i < ans.size(); ++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... |