이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
class convex_hull {
private:
vector<long long> m, b, id;
int ptr = 0;
int bad(int l1, int l2, int l3) {
return ((b[l3] - b[l1]) * (m[l1] - m[l2]) < (b[l2] - b[l1]) * (m[l1] - m[l3]));
}
public:
void add(long long _a, long long _b, int _id) {
m.push_back(_a);
b.push_back(_b);
id.push_back(_id);
while ((int) m.size() >= 3 && bad((int) m.size() - 3, (int) m.size() - 2, (int) m.size() - 1)) {
m.erase(prev(prev(m.end())));
b.erase(prev(prev(b.end())));
id.erase(prev(prev(id.end())));
}
}
pair<int, long long> query(long long x) {
if (ptr >= (int) m.size()) {
ptr = (int) m.size() - 1;
}
while (ptr < (int) m.size() - 1 && m[ptr + 1] * x + b[ptr + 1] >= m[ptr] * x + b[ptr]) {
ptr++;
}
return {id[ptr], m[ptr] * x + b[ptr]};
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<long long> a(n + 1);
vector<long long> pre(n + 1, 0);
for (int i = 1; i <= n; i++) {
cin >> a[i];
pre[i] = pre[i - 1] + a[i];
}
vector< vector<long long> > dp(n + 1, vector<long long>(k + 1, 0));
vector<convex_hull> v(k + 1);
vector< vector<int> > from(n + 1, vector<int>(k + 1, -1));
for (int i = 1; i <= n; i++) {
if (i > 1) {
for (int t = 1; t <= k; t++) {
auto tmp = v[t - 1].query(pre[i]);
from[i][t] = tmp.first;
dp[i][t] = tmp.second;
}
}
for (int t = 0; t < k; t++) {
v[t].add(pre[i], dp[i][t] - pre[i] * pre[i], i);
}
}
cout << dp[n][k] << '\n';
int cur = from[n][k], cnt = 1;
vector<int> ans;
while (cur != -1) {
ans.push_back(cur);
cur = from[cur][k - cnt++];
}
reverse(ans.begin(), ans.end());
for (auto& x : ans) {
cout << x << ' ';
}
cout << '\n';
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... |