이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define llint long long
void openFile() {
ios_base :: sync_with_stdio(false);
cin.tie(NULL);
#ifdef Tr___
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
#endif
}
const int maxN = 1e5 + 5;
const int maxM = 2e2 + 5;
const llint INF = 1e9 + 7;
int N, K;
llint a[maxN];
llint f[maxN][2];
struct line {
llint a, b;
int id;
line() {}
line(llint a, llint b, int id): a(a), b(b), id(id) {}
} l[maxN];
int nLines = 0;
int last = 0;
inline void enter() {
scanf("%d %d", &N, &K);
for (int i = 1; i <= N; ++i) {
scanf("%lld", &a[i]);
a[i] += a[i - 1];
}
}
inline void clear() {
nLines = 0;
last = 1;
}
inline bool bad(const line &a, const line &b, const line &c) {
return (a.b - c.b) * (-a.a + b.a) <= (c.a - a.a) * (-b.b + a.b);
}
inline void update(const line &o) {
while (nLines >= 2
&& bad(l[nLines - 1], l[nLines], o))
--nLines;
l[++nLines] = o;
}
inline llint get(int id, llint x) {
return l[id].a * x + l[id].b;
}
inline int query(llint x) {
//so important!!!
if (last > nLines)
last = nLines;
last = max(last, 1);
if (nLines == 0) return 0;
while (last < nLines && get(last, x) < get(last + 1, x)) ++last;
return last;
}
int t[maxN][maxM];
inline void solve() {
int la;
for (int k = 1; k <= K; ++k) {
clear();
for (int i = k; i <= N; ++i) {
la = query(a[i]);
f[i][k&1] = get(la, a[i]);
t[i][k] = l[la].id;
update(line(a[i], f[i][!(k&1)] - a[i] * a[i], i));
}
}
printf("%lld\n", f[N][K&1]);
int trace = t[N][K];
while (trace) {
printf("%d ", trace);
trace = t[trace][--K];
}
}
int main() {
openFile();
enter();
solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'void enter()':
sequence.cpp:32:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &K);
^
sequence.cpp:34:29: 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... |