이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int K = 205;
const int N = 100005;
struct line {
int id;
ll m, c;
};
int n, k, ptr;
ll a[N], f[K][N];
int pre[K][N];
vector <line> v[K];
stack <int> opts;
// dp(i, k) = dp(j, k - 1) + a[j] * a[i] - a[j]^2
// y = m1x + c1, y = m2x + c2, y = m3x + c3
// x = (c1 - c2)/(m2 - m1)
// x = (c1 - c3)/(m3 - m1)
// (c1 - c3) * (m2 - m1) < (c1 - c2) * (m3 - m1)
bool bad (line i, line j, line k) {
return (i.c - k.c) * (j.m - i.m) < (i.c - j.c) * (k.m - i.m);
}
void add (int at, line l) {
v[at].push_back(l);
while (v[at].size() >= 3 and bad(v[at][v[at].size() - 3], v[at][v[at].size() - 2], v[at][v[at].size() - 1])) {
v[at].erase(v[at].end() - 2);
}
}
line query (int at, ll x) {
while (ptr < v[at].size() - 1 and v[at][ptr].m * x + v[at][ptr].c < v[at][ptr + 1].m * x + v[at][ptr + 1].c) {
++ptr;
}
return v[at][ptr];
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; ++i) {
scanf("%lld", a + i);
a[i] += a[i - 1];
}
for (int i = 1; i <= n; ++i) {
add(0, {i, a[i], -a[i] * a[i]});
}
for (int i = 1; i <= k; ++i) {
ptr = 0;
add(i - 1, {0, 0, 0});
for (int j = 1; j <= n; ++j) {
line opt = query(i - 1, a[j]);
f[i][j] = opt.m * a[j] + opt.c;
pre[i][j] = opt.id;
add(i - 1, {j, a[j], f[i - 1][j] - a[j] * a[j]});
}
}
printf("%lld\n", f[k][n]);
for (int i = k, j = n; i; --i) {
j = pre[i][j];
opts.push(j);
}
while (!opts.empty()) {
printf("%d ", opts.top());
opts.pop();
}
puts("");
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'line query(int, ll)':
sequence.cpp:40:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (ptr < v[at].size() - 1 and v[at][ptr].m * x + v[at][ptr].c < v[at][ptr + 1].m * x + v[at][ptr + 1].c) {
^
sequence.cpp: In function 'int main()':
sequence.cpp:47:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &k);
^
sequence.cpp:49:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", a + 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... |