이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define evaluate(pt, x) (pt.first.first * x + pt.first.second)
const int N = 1e5, K = 205;
int a[N], s[N], p[K], n, k, dp[N][K], last[N][K];
vector<pair<pair<int, int>, int>> hull[K];
int find(int i, int j, int x) {
p[j] = min(p[j], (int)hull[j].size() - 1);
for (; p[j] < (int)hull[j].size() - 1; p[j]++) {
if (evaluate(hull[j][p[j]], x) > evaluate(hull[j][p[j] + 1], x))
break;
}
last[i][j + 1] = hull[j][p[j]].second;
return evaluate(hull[j][p[j]], x);
}
bool inter(pair<int, int> a, pair<int, int> b, pair<int, int> c) {
return (a.second - b.second) * (c.first - a.first) > (a.second - c.second) * (b.first - a.first);
}
void insert(int j, pair<pair<int, int>, int> nl) {
while (hull[j].size() > 1) {
auto temp = hull[j].back();
hull[j].pop_back();
if (inter(nl.first, temp.first, hull[j].back().first)) {
hull[j].push_back(temp);
break;
}
}
hull[j].push_back(nl);
}
int solveTestCase() {
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
s[0] = a[0];
for (int i = 1; i < n; i++) s[i] = s[i - 1] + a[i];
hull[0].push_back({{a[0], -a[0] * a[0]}, 0});
for (int i = 1; i < n; i++) {
for (int j = 1; j <= k && j <= i; j++) {
dp[i][j] = find(i, j - 1, s[i]);
insert(j - 1, {{s[i], dp[i][j - 1] - s[i] * s[i]}, i});
}
insert(min(i, k), {{s[i], dp[i][min(i, k)] - s[i] * s[i]}, i});
}
cout << dp[n - 1][k] << "\n";
for (int i = last[n - 1][k], j = k; j > 0; --j, i = last[i][j]) cout << i + 1 << " ";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int test = 1;
// cin >> test;
while (test--)
solveTestCase();
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'long long int solveTestCase()':
sequence.cpp:55:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
# | 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... |